php – Eloquent:在模型和他的父母上挂钩’保存’事件
发布时间:2020-12-13 21:53:38 所属栏目:PHP教程 来源:网络整理
导读:拥有这个父母: class BaseModel extends Eloquent { protected static $rules = []; public static function boot() { parent::boot(); static::saving(function($model) { return $model-validate(); // - this executes }); }} 我怎样才能在儿童模特身上
拥有这个父母:
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().我需要两个! 解决方法
我找到了解决方案,实际上非常简单.
以下是* ing Eloquent事件的行为,具体取决于返回类型: >返回null或不返回:将保存模型或执行下一个保存回调 所以,这个问题的解决方案很简单: 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 }); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |