Laravel:如何在保存或更新之前或之后创建函数 [英] Laravel: how to create a function After or Before save|update
本文介绍了Laravel:如何在保存或更新之前或之后创建函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要生成一个函数在save()或update()之后或之前调用,但我不知道该怎么做. 我想我需要从save()update()进行回调,但我不知道该怎么做. 谢谢
I need to generate a function to call after or before save() or update() but i don't know how to do. I think I need a callback from save() update() but I don't know how to do. Thanks
推荐答案
在模型内部,您可以添加 boot()方法,该方法将允许您管理这些事件.
Inside your model, you can add a boot() method which will allow you to manage these events.
例如,具有User.php模型:
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
});
}
}
您可以在此处查看所有可用事件: https://laravel.com/docs/5.2 /eloquent#events
You can review all available events over here: https://laravel.com/docs/5.2/eloquent#events
这篇关于Laravel:如何在保存或更新之前或之后创建函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文