Where should I put model saving event listener in laravel 5.1

11,795

Solution 1

Yes that's correct, the EventServiceProvider is the best place for it.

However you can create Observers to keep it clean. I will give you a quick example.

EventServiceProvider

<?php 

namespace App\Providers;

use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

use App\Models\Users;
use App\Observers\UserObserver;

/**
 * Event service provider class
 */
class EventServiceProvider extends ServiceProvider
{
    /**
     * Boot function
     *
     * @param DispatcherContract $events
     */
    public function boot(DispatcherContract $events)
    {
        parent::boot($events);

        Users::observe(new UserObserver());
    }
}

UserObserver

<?php

namespace App\Observers;

/**
 * Observes the Users model
 */
class UserObserver 
{
    /**
     * Function will be triggerd when a user is updated
     * 
     * @param Users $model
     */
    public function updated($model)
    {
    }
}

The Observer will be the place where the saved, updated, created, etc.. functions will be executed.
More information about Observers: http://laravel.com/docs/5.0/eloquent#model-observers

Solution 2

You can register listener callbacks in your models boot method, e.g.:

class User extends Eloquent {
  protected static function boot() {
    parent::boot();

    static::deleting(function ($user) {
      // deleting listener logic
    });

    static::saving(function ($user) {
      // saving listener logic
    });
  }
}
Share:
11,795
refear99
Author by

refear99

Updated on July 23, 2022

Comments

  • refear99
    refear99 almost 2 years

    The Laravel docs say I should put the model events in the EventServiceProvider boot() method like this.

    public function boot(DispatcherContract $events)
    {
    
        Raisefund::saved(function ($project) {
            //do something
        });
    
    }
    

    But I have many models that I want to listen to.
    So I was wondering if it is the right way to put it all in the EventServiceProvider.

  • Jeremie Ges
    Jeremie Ges over 8 years
    where you put the UserObserver (the name of the folder, the name of the file ...) ?
  • Szenis
    Szenis over 8 years
    @JeremieGes I created a new folder in the App directory called Observers. After I did that, I created a file called UserObserver.php inside the directory App\Oberservers.
  • Jeremie Ges
    Jeremie Ges over 8 years
    Do I need to register something in composer.json ?
  • Szenis
    Szenis over 8 years
    @JeremieGes No, you don't have to. You only need to add Users::observe(new UserObserver()); to the EventServiceProvider. After that it should work. You can test it out with the created function. Just put something like die('test'); inside the function to see if it gets triggerd.
  • Ramil Amerzyanov
    Ramil Amerzyanov about 8 years
    it is not laravel way
  • omarjebari
    omarjebari about 7 years
    It's true that the documentation recommends an alternative approach but it doesn't explicitly say that this is bad. To some extent it's clearer here as i can look at my model and i know which events have listeners as opposed to rooting around in a service provider which points to an observer somewhere. Perhaps if you have a lot of listeners it is better elsewhere though.
  • David Lundquist
    David Lundquist almost 7 years
    I agree with Omar. sometimes you have got to back away from being too fanatical. this solution nicely solves the dilemma in a way that won't leaving you going WTF in 8months when you need to tweak something.