脚本宝典收集整理的这篇文章主要介绍了php – Eloquent:在模型和他的父母上挂钩’保存’事件,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
class BaSEModel extends Eloquent { PRotected static $rules = []; public static function boot() { parent::boot(); static::saving(function($model) { return $model->validate(); // <- this executes }); } }
我怎样才能在儿童模特身上做同样的事情?
class Car extends BaseModel { protected static $rules = []; public static function boot() { parent::boot(); static::saving(function($model) { $model->DOStuff(); // <- this doesn't execute }); } }
仅当我删除父项上的saving()时,才会执行子项中的saving().我需要两个!
@H_403_4@
以下是* ing Eloquent事件的行为,具体取决于返回类型:
>返回null或不返回:将保存模型或执行下一个保存回调
> return true:将保存模型,但不会执行下一个保存回调
> return false:不保存模型,也不会执行下一个保存回调
所以,这个问题的解决方案很简单:
class BaseModel extends Eloquent { protected static $rules = []; public static function boot() { parent::boot(); static::saving(function($model) { if(!$model->validate()) return false; // only return false if validate() fails,otherwise don't return anything }); } }@H_403_4@ @H_403_4@
以上是脚本宝典为你收集整理的php – Eloquent:在模型和他的父母上挂钩’保存’事件全部内容,希望文章能够帮你解决php – Eloquent:在模型和他的父母上挂钩’保存’事件所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。