jeudi 31 mars 2016

Laravel 5 isDirty() always returns false

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');
            }
        }
    }



via Chebli Mohamed

1 commentaire:

  1. it works with save() method :
    -----------------------

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

    foreach ($partnerData as $key => $value) {
    $partner->$key = $value;
    }

    if($partner->isDirty()){
    //your code here
    }
    //then save your values
    $partner->save();
    }
    }

    RépondreSupprimer