php 在 Laravel 中 isDirty() 是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28836013/
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
What does isDirty() mean in Laravel?
提问by shay.k
First of all, I am not familiar with Laravel so much (or with the term "dirty" for that matter).
I stumbled upon this line of code -
首先,我对 Laravel 不太熟悉(或者“脏”这个词)。
我偶然发现了这行代码 -
if ($this->isDirty('status')) {
if (Notification::has('website-status-' . strtolower($this->status))) {
Notification::set($this->account, 'website-status-' . strtolower($this->status), $this->emailAttributes())
->email();
}
}
And I couldn't understand what that means exactly. I tried to find out on the internet but the Laravel site only says this
我无法理解这究竟意味着什么。我试图在互联网上找到,但 Laravel 网站只说这个
"Determine if a given attribute is dirty"
“确定给定的属性是否脏”
which doesn't really help...
这并没有真正的帮助......
回答by nickforall
When you want to know if the model has been edited since it was queried from the database, or isn't saved at all, then you use the ->isDirty()
function.
如果您想知道模型自从数据库查询后是否已被编辑,或者根本没有保存,那么您可以使用该->isDirty()
函数。
回答by kapitan
As support for the accepted answer:
作为对已接受答案的支持:
$model = Model::find(1);
$model->first_column = $request->first_value;
$model->second_column = $request->second_value;
$model->third_column = $request->third_value;
if($model->isDirty()){
// the model has been edited, else codes here will not be executed
}
$model->save();