Laravel 为model触发事件

有时候我们需要在数据

class DemoModel extends Model
{
    protected $table = 'tax_source';

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

        static::creating(function ($model) {
            $model->adminid = auth()->user()->getAuthIdentifier();
            $model->adminname = auth()->user()->user->name;
        });
    }
}

其中,可用的事件有:

saving,saved,updating,updated,creating,created,deleting,deleted。

saving对应的函数为:\Illuminate\Database\Eloquent\Model::saving,其他的类似,参数均为:

/**
 * @prams $model Model 当前操作的model
 */
function ($model) {
}

理论上并不是一定需要在boot函数注册这些事件。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注