Intro
I’ve done a quick test to exercise a new-to-me part of the Laravel (5.6) Eloquent API:
$model->isDirty(); $model->isDirty("field_name"); $model->isDirty(["field_name", "another_field_name"]); $model->getDirty();
I want to know when ->isDirty() returns TRUE…
My model
I’ve got an Eloquent model for my contacts table:
namespace App\Contacts; use Illuminate\Database\Eloquent\Model; class Contact extends Model { // ... }
Testing…
$contact = new Contact; echo($contact->isDirty()); // false $contact->first_name = "Mek"; echo($contact->isDirty()); // true echo($contact->isDirty("last_name")); // false echo($contact->isDirty(["first_name", "last_name"])); // true $contact->save(); echo($contact->isDirty()); // false
- isDirty() seems always to return boolean TRUE/FALSE regardless of what attributes you pass: whether you want to check globally (no attributes == any field), for changes to one field (string attribute == field name), or for changes to any number of fields (array attribute == array of field names)
- Works predictably, as expected, whether I use a model instance corresponding to a new record or an existing record.
- Also (extra test, not shown here): if I change a field to a different value, $contact->isDirty() returns TRUE; but if I “change” a field to the same value it had before, $contact->isDirty() returns FALSE – which is good: it’s not returning false positives.