Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jaracas's avatar

Prevent a SPECIFIC observer/event from firing when updating/saving a model?

I have two observers that trigger on updating a model, however for one specific case, in one file, in one method - I need only one of the observers to fire when updating a model.

Is there a way to do this?

0 likes
3 replies
Snapey's avatar
Snapey
Best Answer
Level 122

perhaps a property on the model which you set before updating, then in the observer if this flag is set, it bypasses the function

$model->withoutFoo=true

observer

if($this->withoutFoo) return;
1 like
AM_Bolt's avatar

My solution is

class MyObserver
{
    protected static bool $isEnabled = false;

    public static function disable(): void
    {
        static::$isEnabled = false;
    }

    public static function enable(): void
    {
        static::$isEnabled = true;
    }

    public function created(Model $model)
    {
        if (! static::$isEnabled ) {
            return;
        }

       	// handle created
    }
}

and when need to disable

MyObserver::disable();
(new Model())->save();
MyObserver::enable();

also you can move disabling observer logic to trait

Please or to participate in this conversation.