Laravel:如何在保存|更新之后或之前创建函数 [英] Laravel: how to create a function After or Before save|update

查看:21
本文介绍了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屋!

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