Laravel 5 isDirty()总是返回false [英] Laravel 5 isDirty() always returns false

查看:628
本文介绍了Laravel 5 isDirty()总是返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我的代码:


我想要检查模型是否已经改变了isDirty方法,但总是返回false。

  if(!is_null($ partnersData)){
foreach($ partnersData as $ partnerData){
$ partner = Partner :: find ($ partnerData [ 'PARTNER_ID']);
$ partner-> update($ partnerData);

if($ partner-> isDirty()){
dd('true');
}
}
}


解决方案

$ model-> update()更新并保存模型。因此,自上次执行的查询(查询数据库以保存模型后),模型尚未更改,因此 $ model-> isDirty()等于false。 p>

尝试更新模型,如下所示:

  $ partner = Partner ::找到($ ID); 

foreach($ partnerData as $ column => $ value){
if($ column ==='id')continue;

$ partner-> $ column = $ value;
}

if($ partner-> isDirty()){
//现在应该是脏了
}

$ partner - >保存(); // $ partner will be not-dirty from here


I want to check if the model has been changed with isDirty method, but always returns false.

This is my code :

 if (!is_null($partnersData)) {
        foreach ($partnersData as $partnerData) {
            $partner = Partner::find($partnerData['partner_id']);
            $partner->update($partnerData);

            if($partner->isDirty()){
                dd('true');
            }
        }
    }

解决方案

$model->update() updates and saves the model. Therefore, $model->isDirty() equals false as the model has not been changed since the last executed query (which queries the database to save the model).

Try updating the model like this:

$partner = Partner::find($id);

foreach ($partnerData as $column => $value) {
    if ($column === 'id') continue;

    $partner->$column = $value;
}

if ($partner->isDirty()) {
    // should be dirty now
}

$partner->save(); // $partner will be not-dirty from here

这篇关于Laravel 5 isDirty()总是返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆