Reputation: 1853
Is there a way to remove a model observer that is added with
$model->observe(new ObserverObject)
Maybe something like
$model->observers['ObserverObject']->remove()
Thanks
Upvotes: 4
Views: 5576
Reputation: 2708
Laravel 5.8
I have an AccountObserver, that observers the creating process on my AccountModel:
class AccountObserver
{
public function creating(AccountModel $account)
{
....
}
}
To disable this, first load the event dispatcher (via any eloquent model), then tell it what to forget. This works for any observer method:
$eventDispatcher = AccountModel::getEventDispatcher();
$eventDispatcher->forget('eloquent.creating: App\Models\AccountModel');
If you want to remember it again:
AccountModel::observe(AccountObserver::class);
Upvotes: 3
Reputation: 87779
You can check your event name by doing:
dd( $model->getEventDispatcher()->getListeners() );
And remove it using:
$model->getEventDispatcher()->forget($event);
Upvotes: 5