laravel 挂钩到 Eloquent 前和后保存每个模型
本文介绍了laravel 挂钩到 Eloquent 前和后保存每个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 Laravel 和 ORM 的新手.我怎么能在保存任何模型之前和之后连接到 Eloquent 来触发代码?我知道我可以对特定模型执行以下操作,但我正在研究如何为每个模型执行此操作.
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 models but i'm looking at figuring out how to do this for every model.
推荐答案
您可以创建一个扩展 eloquent 的 BaseModel 类,然后让您的所有模型扩展 BaseModel.举个例子:
You can create a BaseModel class that extends eloquent and then have all your models extend BaseModel. Here's an example:
这篇关于laravel 挂钩到 Eloquent 前和后保存每个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!