Ped*_*ira 3 php laravel eloquent
拥有这个父母:
class BaseModel extends Eloquent {
protected static $rules = [];
public static function boot() {
parent::boot();
static::saving(function($model) {
return $model->validate(); // <- this executes
});
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能在儿童模特身上做同样的事情?
class Car extends BaseModel {
protected static $rules = [];
public static function boot() {
parent::boot();
static::saving(function($model) {
$model->doStuff(); // <- this doesn't execute
});
}
}
Run Code Online (Sandbox Code Playgroud)
该saving()如果我删除了在孩子仅执行saving()父.我需要两个!
我找到了解决方案,实际上非常简单.
以下是*ingEloquent事件的行为,具体取决于返回类型:
return null或no return:将保存模型或执行下一个saving回调return true:模型将被保存,但下一个saving回调将不会被执行return false:模型不会被保存,下一个saving回调将不会被执行所以,这个问题的解决方案很简单:
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
});
}
}
Run Code Online (Sandbox Code Playgroud)