Mute and Unmute Model Observers With Unobserve

Packages

October 11th, 2019

unobserve-featured.png

Unobserve is a Laravel package by Stephen Lewis to mute and unmute observers at will.

When testing Laravel applications, we frequently need to “silence” events, so as not to trigger additional side-effects. Laravel’s Event::fake method is useful, but muting a specific model observer is still problematic.

Unobserve takes care of that, making it easy to mute and unmute an observer at will.

The usage of this package is simple: you use the package’s CanMute trait on model observers:

1namespace App\Observers;
2 
3use Monooso\Unobserve\CanMute;
4 
5class UserObserver
6{
7 use CanMute;
8}

With the trait in place, you can now mute and unmute your observer:

1UserObserver::mute();
2 
3UserObserver::unmute();

To learn more about this package, get full installation instructions, and view the source code on GitHub at monooso/unobserve.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.