This website uses cookies to track usage of this website and to deliver targetted advertising. Please read our Cookie Policy and Privacy Policy for further details of our usage. Continued use of this website indicates acceptance of our use of these cookies.

Disabling Events in Laravel Model Observer

Disabling Events in Laravel Model Observer

I needed to create a model observer for a recent project to add a value to a model after being edited in Laravel Nova. The ideal event for this was creating but unfortunately due to limitations in a package I was using (MySQL Spatial / API stuff) I couldn’t use this.

So I needed to use created. I also needed to run the event on the model being updated. Uh oh, yep, run-away events.

The created event caused the update event to run, which then caused the update event to run, which then caused the update event to run etc… Not good…

The answer I found was to use the following statement prior to the $model->save() event in the created event…

$model::unsetEventDispatcher();

This has the effect of temporarily disabling further events caused when the created event is fired, preventing the updated event from firing which is what I wanted. I believe there are other ways to do this (Google / Stack Overflow are your friends) but this was the best way I found to do this that worked quickly.


More Web Development, PHP, WordPress and Shopify Posts

Search Posts

Share on Social

Get in touch for a quick, free personalised web design quote

Need some help on your WordPress, Laravel or Shopify project?

Get A Web Quote