好像 laravel boot Trait 不起作用
It seems like laravel boot Trait doesn't work
最近我在使用特征时遇到问题,我之前创建了一些并且它似乎有效,但现在我创建了另一个来对创建事件采取行动,这里是
trait Contributes
{
public static function bootContributes()
{
static::creating(function ($model) {
if (!$model->getKey())
{
Contribution::create([
'contributing_id' => $model->id,
'contributing_type' => $model->class_table,
]);
}
});
}
}
我将它包含在多个模型中,例如 use Contributes;
我认为有些事情我不知道。
我试过 dd($model);
内部特征,它在事件外部有效但在事件内部无效
与上述方法不同,我认为这是一种更好的做法,并且与 laravel 配合使用效果更好:
public static function boot()
{
self::created(
function($model)
{
Contribution::create([
'contributing_id' => $model->id,
'contributing_type' => $model->table
]);
}
);
}
最近我在使用特征时遇到问题,我之前创建了一些并且它似乎有效,但现在我创建了另一个来对创建事件采取行动,这里是
trait Contributes
{
public static function bootContributes()
{
static::creating(function ($model) {
if (!$model->getKey())
{
Contribution::create([
'contributing_id' => $model->id,
'contributing_type' => $model->class_table,
]);
}
});
}
}
我将它包含在多个模型中,例如 use Contributes;
我认为有些事情我不知道。
我试过 dd($model);
内部特征,它在事件外部有效但在事件内部无效
与上述方法不同,我认为这是一种更好的做法,并且与 laravel 配合使用效果更好:
public static function boot()
{
self::created(
function($model)
{
Contribution::create([
'contributing_id' => $model->id,
'contributing_type' => $model->table
]);
}
);
}