Reputation: 2187
I have been using Laravel Observers to handle my app logic whenever my models get deleted/updated/saved. I noticed in some cases these observers don't get triggered. For example if I use
Model::where(active,1)->update([some stuff]);
the observers never get triggered. It is very annoying to have to fetch all records and run a forloop on each instance and call update. Is there a way around this or are there any good practices to handle these scenarios?
Upvotes: 4
Views: 2364
Reputation: 87
Events and listeners instead? You can run the update either raw or query builder, pass those effected rows in the event.
Upvotes: 0
Reputation: 163768
When you do this:
Model::where(active,1)->update([some stuff]);
Query Builder's update()
method is executed instead of Eloquent's update()
method.
If you want to trigger Eloquent events, you need to update rows one by one.
Upvotes: 1