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
});
}
}
Author by
refear99
Updated on July 23, 2022Comments
-
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 theEventServiceProvider
. -
Jeremie Ges over 8 yearswhere you put the UserObserver (the name of the folder, the name of the file ...) ?
-
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 over 8 yearsDo I need to register something in composer.json ?
-
Szenis over 8 years@JeremieGes No, you don't have to. You only need to add
Users::observe(new UserObserver());
to theEventServiceProvider
. After that it should work. You can test it out with thecreated
function. Just put something likedie('test');
inside the function to see if it gets triggerd. -
Ramil Amerzyanov about 8 yearsit is not laravel way
-
omarjebari about 7 yearsIt'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 almost 7 yearsI 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.