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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With