Mute and Unmute Model Observers With Unobserve

Mute and Unmute Model Observers With Unobserve

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:

namespace App\Observers;

use Monooso\Unobserve\CanMute;

class UserObserver
{
    use CanMute;
}

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

UserObserver::mute();

UserObserver::unmute();

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


Filed in: News / packages


Newsletter

Join the weekly newsletter and never miss out on new tips, tutorials, and more.

Laravel News Partners

Laravel Jobs

Senior Laravel Developer (Full-Stack)
Munich
Volunteer Vision GmbH
Full-Stack Developer
Tampa
Nu Image Medical
Senior Software Developer
Remote (US ONLY)/Kenilworth, NJ
Diversified
Senior Back-end Laravel API Developer - Immediate Contract (Full Time)
Remote
ApproveMe // Document Signing
Web Development Project Manager
Houston, TX
Swyft Filings
Experienced Laravel/VueJS Developer (Freelance)
Remote (preferably in the US)
Prosperly LLC
Senior React / Laravel Developer
Remote
The C.A.S.E. Engineering Group