gpt4 book ai didi

php - Laravel 观察者不工作

转载 作者:行者123 更新时间:2023-12-02 06:13:49 25 4
gpt4 key购买 nike

我正在尝试使用 laravel observers 来监听模型事件问题是当我提交表单(更新或创建新记录)时,什么也没发生。我错过了什么吗?

app.php

  'providers' => [
...
App\Providers\CasesManagerServiceProvider::class,
]

CasesManagerServiceProvider.php
class CasesManagerServiceProvider extends ServiceProvider
{

public function boot( )
{

Cases::observe(CasesObserver::class);
}

public function register()
{

}

}

CasesObserver.php
class CasesObserver
{
private $cases;

public function __construct(Cases $cases){
$this->cases = $cases;
}


public function creating(Cases $case)
{
dd('creating');
}

public function saved(Cases $case)
{
dd('saved');
}

public function updating($case)
{
dd('updating');
}
public function updated($case)
{
dd('updated');
}
}

案例.php
class Cases extends Model
{
const UPDATED_AT = 'modified_at';

protected $dispatchesEvents = [
'updating' => CasesObserver::class,
'updated' => CasesObserver::class,
'creating' => CasesObserver::class,
'saved' => CasesObserver::class,
];
}

最佳答案

对我来说,问题是在 register() method! 中注册观察者
所以当我把它放在 boot() method一切顺利!原因是提到的服务提供者中运行方法的顺序hear

希望有用

关于php - Laravel 观察者不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48564400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com