Ignore Laravel observer for a specific save method

2.9k views Asked by At

Can I ignore an observer for a specific save method called?

I have this in the observer:

public function saved(Item $item){
    // event stuff
    $event->save();
}

Then I call a save method on the Item class:

public function update_item_notes()
{
    // item stuff
    $item->save();
}

The method is just to update the row, but the save observer is for when a new row is created...

Currently when updating the row in my method it calls the save observer...

2

There are 2 answers

0
Laurence On

There are three model events

creating and created: called for new data only

updating and updated: called for update data only

saving and saved: called when both new and updated data

So change your model event from saved() to created() and it will only be called for new rows.

1
Abbas Fechit On

You can ignore the observer by using withoutEvents and saveQuietly methods see the documentation here