Repository navigation
Usage
Victor Tesoura Júnior edited this page Mar 21, 2024
·
2 revisions
Let's suppose that you have an
usermodel
Following the example, to create an observer run the artisan command below.
php artisan make:observer UserObserverThis command will place the new observer in your app/Observers directory. If this directory does not exist, Artisan will create it for you.
After creating the observer, you should:
- Extend the
CoreObserver; - Invoke the
CRUDMethodsObserverandSoftDeleteMethodsObservertraits; - Define the
$identifiervalue, which I recommend to use the same model name (Eg: "users").
<?php
namespace App\Observers;
use Txsoura\ActivityLog\Observers\CoreObserver;
use Txsoura\ActivityLog\Observers\Traits\CRUDMethodsObserver;
use Txsoura\ActivityLog\Observers\Traits\SoftDeleteMethodsObserver;
class UserObserver extends CoreObserver
{
use CRUDMethodsObserver, SoftDeleteMethodsObserver;
protected string $identifier = 'user';
}You may register observers in the boot() method of your application's App\Providers\EventServiceProvider.php service provider:
/**
* Register any events for your application.
*/
public function boot(): void
{
//Register all observers here ...
User::observe(UserObserver::class);
}Great codes to reuse everywhere you want 🚀