Reputation: 191
When a model of me is updated an event is triggered:
protected $events = [
'updated' => ZoneUpdated::class,
];
The event listener which is triggered will create on the model too:
public function handle(ZoneUpdated $event)
{
Zone::find($event->zone->id)->update([
'updated' => true,
'valid' => 'u',
]);
}
How can I disable the listener to fire a new event, because at the moment this produces an infinite loop.
Upvotes: 0
Views: 817
Reputation: 173
There are ways to make sure events only fire on certain conditions, but I find a simpler way to circumvent this would be to use the updating
event instead of update
:
protected $events = [
'updating' => ZoneUpdated::class,
];
Upvotes: 0
Reputation: 151
If you still need to keep track of the Model Updated event and still continue to update that Model, you can try to update the Model in in Listener using Fluent
public function handle(ZoneUpdated $event)
{
// assuming table name for Zone Model is "zones" and primary key is "id"
\DB::table('zones')
->where('id', $event->zone->id)
->update([
'updated' => true,
'valid' => 'u',
]);
}
Using fluent should stop the infinite loop as it no longer will execute the update based on the Model Object but directly to the Database via Fluent.
Having said that, I would urge you to rethink how you are using event listener and if this is really what you need.
Upvotes: 2