保存 Laravel 4 时检测更改:Eloquent
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20179711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detect changes when saving Laravel 4: Eloquent
提问by Karl
I'm using the Laravel 4 framework and I am trying to work out a way to display notifications depending on whether a save()
was successful or not. Here's what I have so far:
我正在使用 Laravel 4 框架,我正在尝试找出一种根据 asave()
是否成功来显示通知的方法。这是我到目前为止所拥有的:
if($user->save()) {
Session::flash('success','woohoo success');
} else {
Session::flash('error','uhoh error');
}
return Redirect::action('UsersController@show', array('users' => $id));
My problem is that it always returns true when a user is saved, even if no changes have been made to the database (I know this from the updated_at timestamp). Is there a way to detect whether any changes have actually been made or not using Laravel?
我的问题是它总是在保存用户时返回 true,即使没有对数据库进行任何更改(我从 updated_at 时间戳中知道这一点)。有没有办法使用 Laravel 检测是否实际进行了任何更改?
回答by Gadoma
Depending on what you want to achieve, you have some options here. Check out the below first.
根据您想要实现的目标,您在此处有一些选择。请先查看以下内容。
Model Hooks with Ardent package
If you are interested in autovalidating models, take a look at the https://github.com/laravelbook/ardentpackage - Self-validating smart models for Laravel Framework 4's Eloquent O/RM. Apart form great validation features it offers additional model hooks you can use:
带有 Ardent 包的模型挂钩
如果您对自动验证模型感兴趣,请查看https://github.com/laravelbook/ardent包 - Laravel Framework 4 的 Eloquent O/RM 的自验证智能模型。除了强大的验证功能外,它还提供了额外的模型挂钩,您可以使用:
Here's the complete list of available hooks:
before/afterCreate()
before/afterSave()
before/afterUpdate()
before/afterDelete()
before/afterValidate() - when returning false will halt validation, thus making save() operations fail as well since the validation was a failure.
Laravel Model Events
If you dont want to use any additional stuff, you can just use the Laravel Model Events (that in fact Ardent is wrapping in the hooks). Look into the docs http://laravel.com/docs/eloquent#model-events
Laravel 模型事件
如果你不想使用任何额外的东西,你可以只使用 Laravel 模型事件(实际上 Ardent 是用钩子包裹的)。查看文档http://laravel.com/docs/eloquent#model-events
Eloquent models fire several events, allowing you to hook into various points in the model's lifecycle using the following methods: creating, created, updating, updated, saving, saved, deleting, deleted, restoring, restored.
Whenever a new item is saved for the first time, the creating and created events will fire. If an item is not new and the save method is called, the updating / updated events will fire. In both cases, the saving / saved events will fire.
If false is returned from the creating, updating, saving, or deleting events, the action will be cancelled:
Eloquent 模型会触发多个事件,允许您使用以下方法挂钩模型生命周期中的各个点:创建、创建、更新、更新、保存、保存、删除、删除、恢复、恢复。
每当第一次保存新项目时,将触发创建和创建事件。如果项目不是新的并且调用了 save 方法,则将触发更新/更新事件。在这两种情况下,保存/保存的事件都会触发。
如果创建、更新、保存或删除事件返回 false,则该操作将被取消:
Solution
Finally, reffering to you question you can utilize the above approaches in numerous ways but most obviously you can combine it (or not) with the Eloquent Models' getDirty()api docs heremethod. It will work for example with the saving event.
解决方案
最后,针对您的问题,您可以通过多种方式利用上述方法,但最明显的是,您可以将它(或不)与EloquentModels 的getDirty() api docs here方法结合使用。例如,它将与保存事件一起工作。
Yourmodel::saving(function($model)
{
foreach($model->getDirty() as $attribute => $value){
$original= $model->getOriginal($attribute);
echo "Changed $attribute from '$original' to '$value'<br/>\r\n";
}
return true; //if false the model wont save!
});