-
-
Notifications
You must be signed in to change notification settings - Fork 0
Usage
Victor Tesoura Júnior edited this page Mar 21, 2024
·
2 revisions
Let's suppose that you have an
user
model
Following the example, to create an observer run the artisan command below.
php artisan make:observer UserObserver
This 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
CRUDMethodsObserver
andSoftDeleteMethodsObserver
traits; - Define the
$identifier
value, 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 🚀