PHP code example of akaunting / laravel-mutable-observer
1. Go to this page and download the library: Download akaunting/laravel-mutable-observer library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
akaunting / laravel-mutable-observer example snippets
namespace App\Observers;
use Akaunting\MutableObserver\Traits\Mutable;
class UserObserver
{
use Mutable;
public function creating($user)
{
// Send welcome email
Mail::to($user->email)->send(new WelcomeEmail($user));
}
public function created($user)
{
// Log user creation
Log::info("User created: {$user->email}");
}
public function updating($user)
{
// Validate changes
// ...
}
}
UserObserver::mute();
// Create users without triggering any observer events
$user = User::create([
'name' => 'John Doe',
'email' => '[email protected]',
]);
// No emails sent, no logs created
UserObserver::unmute();
// Mute only the creating event (no welcome emails)
UserObserver::mute('creating');
$user = User::create(['name' => 'Jane Doe']);
// Creating event is muted, but created event still fires
// Mute multiple specific events
UserObserver::mute(['creating', 'updating']);
$user = User::create(['name' => 'Test User']);
$user->update(['name' => 'Updated Name']);
// Both creating and updating events are muted
UserObserver::unmute();
use Tests\TestCase;
class UserTest extends TestCase
{
public function test_user_can_be_created_without_side_effects()
{
// Prevent emails and logs during testing
UserObserver::mute();
$user = User::factory()->create();
$this->assertDatabaseHas('users', [
'email' => $user->email,
]);
UserObserver::unmute();
}
public function test_only_email_is_muted()
{
// Mute only email sending, keep logging
UserObserver::mute('creating');
Log::shouldReceive('info')->once();
$user = User::factory()->create();
// Email not sent, but log was created
UserObserver::unmute();
}
}
use Illuminate\Support\Facades\App;
// Mute observers in specific environments
if (App::environment('testing')) {
UserObserver::mute();
}
// Or use it for specific operations
$shouldNotify = false;
if (!$shouldNotify) {
UserObserver::mute('creating');
}
$user = User::create($data);
if (!$shouldNotify) {
UserObserver::unmute();
}
use Akaunting\MutableObserver\Traits\Mutable;
// Use the built-in wildcard constant
UserObserver::mute(Mutable::WILDCARD_EVENT); // Same as mute()
UserObserver::mute(); // Mute all events
UserObserver::mute('created'); // Mute single event
UserObserver::mute(['created', 'updated']); // Mute multiple events
UserObserver::unmute(); // Restore all observer functionality
try {
UserObserver::mute();
// Your code here
} finally {
UserObserver::unmute();
}
protected function setUp(): void
{
parent::setUp();
UserObserver::mute();
}
protected function tearDown(): void
{
UserObserver::unmute();
parent::tearDown();
}
UserObserver::mute('creating'); // Better than mute()
// Mute email notifications during bulk import
UserObserver::mute('created');
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.