laravel hook into Eloquent pre and post save for every model

前端 未结 3 1853
我寻月下人不归
我寻月下人不归 2020-12-31 13:54

I\'m new to Laravel and ORM\'s in general. How could i hook into Eloquent to fire code before and after a save of any model? I know i can do the following for specific model

3条回答
  •  再見小時候
    2020-12-31 14:20

    Using laravel models own life cycle events may solve this easy

    /**
     * model life cycle event listeners
     */
    public static function boot(){
        parent::boot();
    
        static::creating(function ($instance){
            //
        });
    
        static::created(function ($instance){
            //
        });
    }
    

提交回复
热议问题