Laravel 5 isDirty() always returns false

后端 未结 1 839
再見小時候
再見小時候 2020-12-06 17:30

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)) {
               


        
相关标签:
1条回答
  • 2020-12-06 18:04

    $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
    
    0 讨论(0)
提交回复
热议问题