我有以下代码,一旦创建模型,该代码便会调度工作
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 App\Models\Foo::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 :: created方法中调用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();
}
}
答案 0 :(得分:0)
您的bar()函数不是静态的。您应该首先使用$ foo = new Foo创建模型。比调用$ foo-> bar()