Laravel在类的静态创建方法中调用模型类方法



我有以下代码,一旦创建模型,它将调度作业

class Foo extends Model {
public static function boot()
{
parent::boot();
static::created(function (Foo $foo) {
dispatch(function() use ($foo) {
$foo->bar();
});
});
}
public function bar()
{
$this->baz = "";
$this->save();
}
}

这是我在日志中遇到的错误

Call to undefined method AppModelsFoo::bar() {"exception":"[object] (BadMethodCallException(code: 0): Call to undefined method App\Models\Foo::bar() at ...../vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php:50)

问:如何在我的 static::create 方法中调用 bar((?

====

========================================================== 到目前为止,我正在使用这种方法,即使我不喜欢这种方法

class Foo extends Model {
public static function boot()
{
parent::boot();
static::created(function (Foo $foo) {
$foo_id = $foo->id;
dispatch(function() use ($foo_id) {
$foo = Foo::find($foo_id);
$foo->bar();
});
});
}
public function bar()
{
$this->baz = "";
$this->save();
}
}

bar (( 函数不是静态的。应首先使用 $foo = new Foo 创建模型。比调用 $foo->bar((

最新更新