'Laravel : one observer for multiple models

I have multiple model that have same logic oncreated method, can I have one observer for that? Or another approach just to avoid creating multiple Observers for each model?



Solution 1:[1]

You may register Closure based events manually in the boot method of your EventServiceProvider:

/**
 * Register any other events for your application.
 *
 * @return void
 */
public function boot()
{
    parent::boot();

    Event::listen(['eloquent.saved: *', 'eloquent.created: *', ...], function($context) {
       // dump($context); ---> $context hold information about concerned model and fired event : e.g "eloquent.created: App\User"
       // YOUR CODE GOES HERE
    });
}

Solution 2:[2]

This is much better when you apply this in provider, so in the future you can easily remove if laravel provide a better way.

currently we using laravel v8.42.1, but this seems applicable in older version.


namespace App\Providers;

use App\Observers\GlobalObserver;
use App\Models\Product;

class ObserverServiceProvider extends ServiceProvider
{
    public function register()
    {
        //
    }

    public function boot()
    {
        self::registerGlobalObserver();
    }

    private static function registerGlobalObserver()
    {
        /** @var \Illuminate\Database\Eloquent\Model[] $MODELS */
        $MODELS = [
            Product::class,
            // ...... more models here
        ];

        foreach ($MODELS as $MODEL) {
            $MODEL::observe(GlobalObserver::class);
        }
    }
}

Then this will be your observer

<?php

namespace App\Observers;

use Illuminate\Database\Eloquent\Model;

class GlobalObserver
{
    public function updated(Model $model)
    {
        // ...
    }

    public function created(Model $model)
    {
        // ...
    }

    public function deleted(Model $model)
    {
        // ...
    }

    public function forceDeleted(Model $model)
    {
       // ...
    }
}

Then register the provider in config/app.php in providers key

    'providers' => [
        \App\Providers\ObserverServiceProvider::class,
        // ...
    ],

the good this about this is you never go to the multiple model class just to modify them

Solution 3:[3]

I would keep an observer per model, but instead extract a Trait/a base class/a service class with the common functionality that you can then use in those observers.

Solution 4:[4]

You can use the Observer class for that purpose https://laravel.com/docs/6.x/eloquent#observers

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 mdexp
Solution 4 Denis Duliçi