Laravel: Listen for Model save or update (after or before they’re done)

Inside your model, you can add a boot() method which will allow you to manage these events.

For example, having User.php model:

class User extends Model 
{

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

        self::creating(function($model){
            // ... code here
        });

        self::created(function($model){
            // ... code here
        });

        self::updating(function($model){
            // ... code here
        });

        self::updated(function($model){
            // ... code here
        });

        self::deleting(function($model){
            // ... code here
        });

        self::deleted(function($model){
            // ... code here
        });
    }

}

You can review all available events over here: https://laravel.com/docs/5.2/eloquent#events

Leave a Comment