cyberfly
1/17/2018 - 4:33 AM

Laravel eloquent create, update, delete event

trait Trackable
{
    public static function bootTrackable()
    {
        static::creating(function ($model) {
            // blah blah
        });

        static::updating(function ($model) {
            // bleh bleh
        });

        static::deleting(function ($model) {
            // bluh bluh
        });
    }
}
class BaseModel extends Model
{
    public static function boot()
    {
        static::creating(function ($model) {
            $model->user_id = auth()->user()->id;
        });

        static::updating(function ($model) {
            // bleh bleh
        });

        static::deleting(function ($model) {
            // bluh bluh
        });
        
        parent::boot();
    }
}