Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Eloquent event inheritance

When I do this:

class Content extends Eloquent {

}

Content::saving(function($content) {
    // do something
});

class Article extends Content {

}

The 'do something' event doesn't fire when saving an Article. Is there any way that Article can inherit this event binding?

like image 764
tprsn Avatar asked Nov 26 '25 13:11

tprsn


1 Answers

You can use late static binding in the parent's static boot method that laravel automatically calls:

class Content extends Eloquent
{

    public static function boot()
    {
        parent::boot();

        static::saving(function ($model) {
            // do something
        });
    }

}
like image 185
Joseph Silber Avatar answered Nov 28 '25 14:11

Joseph Silber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!