Fire same Model Event twice
Question
I'm dealing with the following code:
// BaseModel
abstract class BaseModel extends Eloquent {
public static function boot()
{
parent::boot();
static::saving(function($model)
{
echo 'Fired BaseModel';
});
}
}
// Project (extends BaseModel)
class Project extends BaseModel {
public static function boot()
{
parent::boot();
static::saving(function($model)
{
echo 'Fired Project';
});
}
}
When saving new/existing data to the Project Model, this only gives me Fired BaseModel
. It doesn't fire the Saving Event on the Project Model at all. Is there a way to overcome this limitation?
Solution
Fire the child event first before calling the parent event should solve it
// Project (extends BaseModel)
class Project extends BaseModel {
public static function boot()
{
static::saving(function($model)
{
echo 'Fired Project';
});
parent::boot();
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow