@tag search for model events in laravel docs. You will get what you are looking for.
Listen to *any* save/update/create event for *any* model?
I want to listen for any time a model changes, and I don't care what model it is. I don't want to have to set up a listener for every model, so I'd like to be able to just listen to a generic Model::saved(...) event.
Does such a thing exist?
@tag here it is http://laravel.com/docs/5.1/eloquent#events
@jsartisan, please re-read my post more carefully.
Oh got it. sorry my bad.
Figured it out:
Had to dive deep into the source code to dig this one up:
Event::listen(['eloquent.saved: *', 'eloquent.created: *'], function() {
//
});
Where the asterisk * is a wildcard that matches the name of any model
@tag Great ! :D
@tag hey. Why not extend the Model class and add there the listeners? You can create a BaseModel class:
class BaseModel extends Model
{
public static function boot()
{
static::creating(function ($model) {
// blah blah
});
static::updating(function ($model) {
// bleh bleh
});
static::deleting(function ($model) {
// bluh bluh
});
parent::boot();
}
}
Now make your models inherit from that class.
Alternatively you can create a trait and do that there:
trait Trackable
{
public static function bootTrackable()
{
static::creating(function ($model) {
// blah blah
});
static::updating(function ($model) {
// bleh bleh
});
static::deleting(function ($model) {
// bluh bluh
});
}
}
Now just when you need to track the model, make it use that trait. Much cleaner way.
Easy
Event::listen('eloquent.saved: *', 'file path');
It's tempting to do things in a dynamic way but you are much better off in the long run integrating into the framework and adding traits to models, policies, middleware and providers. Laravel gets its dynamicity via artisan and adding little one liners all over the place. It takes a while to learn it all though.
So yeh make the trait and then apply it to all the models you want to listen to. I thought I'd also suggest an improvement to the code sample above that you can write the function once and then use it as the param to each of the listeners.
Can anyone help how to get table name ?
class Basemodel extends Model {
public static function boot() {
static::creating(function ($model) {
$log = new Log();
$log->action = 'Insert ';
$log->name = $model;
$log->save();
});
static::updating(function ($model) {
$log = new Log();
$log->action = 'update';
$log->name = $model;
$log->save();
});
static::deleting(function ($model) {
$log = new Log();
$log->action = 'delete';
$log->name = $model;
$log->save();
});
parent::boot();
}
}
}
I think $model->getTable() is what you're looking for @maverick.
@KFIRBA - I don't think those events would work that way, whenever I place another event within the same trait, it no longer listens to the remaining part of the trait
@agilasadi do you need to consider laravel bootable traits
https://andy-carter.com/blog/using-laravel-s-eloquent-traits
Please or to participate in this conversation.