Skip to content
Victor Tesoura Júnior edited this page Mar 21, 2024 · 2 revisions

Let's suppose that you have an user model

Defining Observers

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.

Modify the created observer

After creating the observer, you should:

  • Extend the CoreObserver;
  • Invoke the CRUDMethodsObserver and SoftDeleteMethodsObserver 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';
}

Register an observer

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);
    }
Clone this wiki locally