Laravel雄辩事件-实施以保存模型(如果已更新) [英] Laravel Eloquent Events - implement to save model if Updated

查看:96
本文介绍了Laravel雄辩事件-实施以保存模型(如果已更新)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在控制器中有这样的代码:

for($i=0; $i<$number_of_tourists; $i++) {

$tourist = Tourist::updateOrCreate(['doc_number' => $request['doc_number'][$i]],
$tourist_to_update);

}

因此,updateOrCreate方法可以1)更新记录,2)创建一个新记录3)如果$ tourist_to_update等于数据库中已有的记录,则保持记录不变.

我只想将"tourist"表中的记录更新时将$ tourist_to_update保存到$ array []中()(而不是在创建新记录或没有任何更改时).

据我所知,有一些雄辩的事件 https://laravel.com/docs/5.4/eloquent#events 具有更新 updated 事件,这些事件似乎符合我的需求.

问题是我观看并阅读了一些教程,但仍然不了解如何使用它们.

我知道如何在游客模式中注册雄辩事件:

protected static function boot()
{
    parent::boot();

    static::updating(function ($model) {

    });
}

谁能解释我如何通过口才事件实现我的目标?

谢谢.

解决方案

这是一种更为复杂的方法,但是如果您只想获取已更改其数据的更新记录,则此方法有效.在您的控制器方法中添加:

app()->singleton('touristsCollector', function ($app) {
    $collector = new \stdClass;
    $collector->updated = [];

    return $collector;
});

for($i = 0; $i < $number_of_tourists; $i++) {
    $tourist = Tourist::updateOrCreate(
        ['doc_number' => $request['doc_number'][$i]],
        $tourist_to_update
    );
}

$collector = resolve('touristsCollector');
var_dump( $collector->updated ); // Only updated tourists which have data changed

这基本上将向应用容器添加一个标准的PHP类,该类将收集来自模型事件的所有更新的游客.在您的模型中添加以下内容:

protected static function boot()
{
    parent::boot();

    static::updated(function ($model) {
        $collector = resolve('touristsCollector');
        $collector->updated[] = $model;
    });
}

这基本上是从app容器中获取PHP类,仅当它的某些属性已更新时才添加游客模型.

I have such code in the controller:

for($i=0; $i<$number_of_tourists; $i++) {

$tourist = Tourist::updateOrCreate(['doc_number' => $request['doc_number'][$i]],
$tourist_to_update);

}

so, the updateOrCreate method can 1) Update record, 2) Create a new one 3) Leave record untouched if $tourist_to_update equals to what's already in the DB.

I want to save $tourist_to_update into $array[] only when a record in 'tourist" table is being updated (not when a new record created or nothing changes).

As I know, there are Eloquent events https://laravel.com/docs/5.4/eloquent#events which have updating and updated events, which seem to match my needs.

The problem is I watched&read several tutorials and still don't understand how to use them.

I know how-to-register a Eloquent event in Tourist model:

protected static function boot()
{
    parent::boot();

    static::updating(function ($model) {

    });
}

Could anyone explain how can I achive my goal with Eloquent events?

Thank you in advance.

解决方案

This is a bit more complex approach, but if you want to get only updated records which have their data changed this is working. In your controller method add:

app()->singleton('touristsCollector', function ($app) {
    $collector = new \stdClass;
    $collector->updated = [];

    return $collector;
});

for($i = 0; $i < $number_of_tourists; $i++) {
    $tourist = Tourist::updateOrCreate(
        ['doc_number' => $request['doc_number'][$i]],
        $tourist_to_update
    );
}

$collector = resolve('touristsCollector');
var_dump( $collector->updated ); // Only updated tourists which have data changed

This will basically add to the app container a standard PHP class which will collect all the updated tourists from the model event. In your model add this:

protected static function boot()
{
    parent::boot();

    static::updated(function ($model) {
        $collector = resolve('touristsCollector');
        $collector->updated[] = $model;
    });
}

This will basically get the PHP class from the app container add the tourist model only if some properties of it have been updated.

这篇关于Laravel雄辩事件-实施以保存模型(如果已更新)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆