Laravel 5.6-模型事件:您可以将static :: creating和static :: upd结合起来吗?

Che*_*hev 3 php laravel laravel-5

我有模型事件:

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

    static::creating(function ($questionnaire) {
        // Same code here
    });

    static::updating(function ($questionnaire) {
        // Same code here
    });
}
Run Code Online (Sandbox Code Playgroud)

是否有将创建和更新结合在一起的方法,还是将相同的代码放在某种局部中以便在每个事件中重用更好?

Dev*_*von 6

https://laravel.com/docs/5.6/eloquent#events

首次保存新模型时,将触发创建事件和创建事件。如果数据库中已经存在一个模型并且调用save方法,则将触发update / update事件。但是,在两种情况下,都会触发保存/保存事件。

节省创建时,模型或更新事件。