Hirad Roshandel
Hirad Roshandel

Reputation: 2187

Triggering Laravel observers on multiple records

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

Answers (2)

David Latty
David Latty

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

Alexey Mezenin
Alexey Mezenin

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

Related Questions