[英]Laravel observer property not being called on update
I am looking into observers and I found out that instead of calling an observer during the boot()
method of the App\Providers\EventServiceProvider
laravel can call it using the property protected $observers
.我正在调查观察者,我发现不是在
App\Providers\EventServiceProvider
laravel 的boot()
方法期间调用观察者,而是可以使用属性protected $observers
调用它。
It works perfectly fine when calling it in the boot method, but it does not work when I put it in the property.在 boot 方法中调用它时它工作得很好,但是当我把它放在属性中时它不起作用。
What am I doing wrong?我究竟做错了什么? I could not find any other explanation in the documentation.
我在文档中找不到任何其他解释。
My eventServiceProvider:我的事件服务提供商:
class EventServiceProvider extends ServiceProvider
{
/**
* Summary of Observers
* @var mixed
*/
protected $observers = [
User::class => [UserObserver::class],
];
/**
* Register any events for your application.
*
* @return void
*/
public function boot()
{
//User::observe(UserObserver::class);
//Above line is commented because property $observers is used, but uncommented when property observers is commented.
}
My observer:我的观察者:
class UserObserver
{
/**
* Handle the User "updated" event.
*
* @param \App\Models\User $user
* @return void
*/
public function updated(User $user)
{
$dirty = $user->getChanges();
dump($user);
dd($dirty);
}
}
Have you tried calling $this->register();你有没有试过调用 $this->register(); inside the boot function?
在引导 function 内?
class EventServiceProvider extends ServiceProvider
{
/**
* Summary of Observers
* @var mixed
*/
protected $observers = [
User::class => [UserObserver::class],
];
/**
* Register any events for your application.
*
* @return void
*/
public function boot()
{
$this->register();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.