Laravel Bright Model Update Event Does Not Trigger

Happy christmas guys!

I am new to Laravel. I had an initial question when I try to use a service provider and a model event to register update information.

Followed the online doc: https://laravel.com/docs/5.3/eloquent#events

After all the code is compiled, I find that the model event only fires when it is created, but never writes anything when I edit the user.

Did I miss something? Feel that the user $ has not received the intended destination. Where is it from? from another service provider?

Any explanation or hint would be appreciated!

<?php namespace App\Providers; use App\User; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { User::creating(function ($user) { Log::info('event creating'); }); User::created(function ($user) { Log::info('event created'); }); User::updating(function ($user) { Log::info('event updating'); }); User::updated(function ($user) { Log::info('event updated'); }); User::saving(function ($user) { Log::info('event saving'); }); User::saved(function ($user) { Log::info('event saved'); }); User::deleting(function ($user) { Log::info('event deleting'); }); User::deleted(function ($user) { Log::info('event deleted'); }); } /** * Register the service provider. * * @return void */ public function register() { // } } 
+5
source share
2 answers

You need to get the user from the database, and then save this user to fire the event. For instance:

This does NOT fire an update event:

 User::where('id', $id)->update(['username' => $newUsername]); 

This will trigger an update event:

 User::find($id)->update(['username' => $newUsername]); 
+7
source

Possible reasons:

When a bulk update is released via Eloquent, saved and updated model events will not be released for updated models. This is because when a mass update is released, models are never retrieved.

+2
source

Source: https://habr.com/ru/post/1261752/


All Articles