Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nomi's avatar
Level 1

Global Observer for Eloquent Model Events and "DB" class queries Events

Hello Awesome Guys!

I want to make a global events Observer for Eloquent Models and all the queries that are fired through the "DB::" Class.

I have read through the Laravel official documentation at https://laravel.com/docs/10.x/eloquent#events But it seems it single model related Observer which is a tricky situation for me if I want to capture Events for multiple Models (It will lead to code duplication and management headache).

I also to want to capture all the events fired when any query is fired through the DB class.

I wanted to ask if there is any trait I can make which will allow me to capture all the events (For Eloquent and DB) within a single class.

Thanks in Advance

0 likes
1 reply
LaryAI's avatar
Level 58

One solution to achieve this is to create a global observer class that listens to all Eloquent model events and DB queries. Here's an example implementation:

<?php

namespace App\Observers;

use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Support\Facades\DB;

class GlobalObserver
{
    public function created($model)
    {
        // handle created event
    }

    public function updated($model)
    {
        // handle updated event
    }

    public function deleted($model)
    {
        // handle deleted event
    }

    public function retrieved($model)
    {
        // handle retrieved event
    }

    public function queryExecuted(QueryExecuted $query)
    {
        // handle query executed event
    }

    public function register()
    {
        foreach ($this->getModels() as $model) {
            $model::observe($this);
        }

        DB::listen(function (QueryExecuted $query) {
            $this->queryExecuted($query);
        });
    }

    protected function getModels()
    {
        // return an array of all Eloquent models you want to observe
        return [
            \App\Models\User::class,
            \App\Models\Post::class,
            // add more models here
        ];
    }
}

In this example, the GlobalObserver class defines methods for handling all Eloquent model events (created, updated, deleted, and retrieved) as well as a method for handling DB query executed events (queryExecuted). The register method is called to register the observer with all the Eloquent models and the DB query listener.

To use this observer, you can simply call the register method in your AppServiceProvider:

<?php

namespace App\Providers;

use App\Observers\GlobalObserver;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        GlobalObserver::register();
    }
}

This will register the GlobalObserver with all the Eloquent models and the DB query listener, allowing you to capture all events in a single class.

Please or to participate in this conversation.