laravel model callbacks after save, before save, etc

后端 未结 7 2088
傲寒
傲寒 2020-11-30 09:34

Are there callbacks in Laravel like:

afterSave()
beforeSave()
etc

I searched but found nothing. If there are no such things - what is best

7条回答
  •  一整个雨季
    2020-11-30 10:05

    Actually, Laravel has real callback before|after save|update|create some model. check this:

    https://github.com/laravel/laravel/blob/3.0/laravel/database/eloquent/model.php#L362

    the EventListener like saved and saving are the real callbacks

    $this->fire_event('saving'); 
    
    $this->fire_event('saved');
    

    how can we work with that? just assign it to this eventListener example:

     \Laravel\Event::listen('eloquent.saving: User', function($user){
      $user->saving();//your event or model function
    });
    

提交回复
热议问题