保存后,保存前等的laravel模型回调 [英] laravel model callbacks after save, before save, etc

查看:131
本文介绍了保存后,保存前等的laravel模型回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Laravel中是否存在回调,如:

Are there callbacks in Laravel like:

afterSave()
beforeSave()
etc

我搜索了但什么都没找到.如果没有这样的东西-实施它的最佳方法是什么?

I searched but found nothing. If there are no such things - what is best way to implement it?

谢谢!

推荐答案

实际上,Laravel在保存|更新|创建某些模型之前|之后具有真正的回调.检查一下:

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

保存和保存的EventListener都是真正的回调

the EventListener like saved and saving are the real callbacks

$this->fire_event('saving'); 

$this->fire_event('saved');

我们该如何处理?只需将其分配给此eventListener示例:

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
});

这篇关于保存后,保存前等的laravel模型回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆