32

First of all, I am not familiar with Laravel so much (or with the term "dirty" for that matter).
I stumbled upon this line of code -

if ($this->isDirty('status')) {
    if (Notification::has('website-status-' . strtolower($this->status))) {
        Notification::set($this->account, 'website-status-' . strtolower($this->status), $this->emailAttributes())
            ->email();
    }
}

And I couldn't understand what that means exactly. I tried to find out on the internet but the Laravel site only says this

"Determine if a given attribute is dirty"

which doesn't really help...

Salim Djerbouh
  • 10,719
  • 6
  • 29
  • 61
shay.k
  • 503
  • 1
  • 7
  • 15
  • 8
    http://stackoverflow.com/questions/18498518/how-to-check-if-a-record-is-new-in-laravel "If you wish to know if the model has been modified since being grabbed from the database, or simply not saved at all (aka if it needs saving) then you can use the ->isDirty() function" – ka_lin Mar 03 '15 at 15:50
  • 3
    When a record is fetched from the database, it is 'clean'. After you modify it, it becomes 'dirty'. Once you save it back to the database, it becomes 'clean' again. – Jeff Lambert Mar 03 '15 at 16:09

4 Answers4

45

When you want to know if the model has been edited since it was queried from the database, or isn't saved at all, then you use the ->isDirty() function.

nickforall
  • 556
  • 6
  • 7
  • 9
    You just copy/pasted `ka_lins` comment almost word for word without acknowledgment? – Adam Sep 21 '18 at 06:55
  • 7
    @Adam he doesn't have to, [read this](https://meta.stackoverflow.com/a/288849/5581565) – Salim Djerbouh Sep 22 '19 at 19:34
  • 1
    http://prntscr.com/r7sxgq Any idea this method returning me TRUE but it's not changes any single value I think. – M Amir Shahzad Feb 26 '20 at 08:12
  • @MAmirShahzad what returns when you call ->getChanges()? I got weird behaviour when save decimal(8,2) values on database. (type casting detect as changes). To solve this I use number_format($variable, 2, '.', '') – Tomir Schmite Jr. Oct 22 '22 at 19:26
25

The isDirty method determines if any attributes have been changed since the model was loaded. You may pass a specific attribute name to determine if a particular attribute is dirty.

    $user = User::create([
        'first_name' => 'Amir',
        'last_name' => 'Kaftari',
        'title' => 'Developer',
    ]);

    $user->title = 'Jafar';
    $user->isDirty(); // true
    $user->isDirty('title'); // true
    $user->isDirty('first_name'); // false
Amir Kaftari
  • 1,305
  • 14
  • 13
12

Eloquent provides the isDirty, isClean, and wasChanged methods to examine the internal state of your model and determine how its attributes have changed from when they were originally loaded.

You can find complete description and examples of these three methods here in the official document: https://laravel.com/docs/9.x/eloquent#examining-attribute-changes

Moslem Deris
  • 196
  • 1
  • 7
9

As support for the accepted answer:

$model = Model::find(1);

$model->first_column = $request->first_value;
$model->second_column = $request->second_value;
$model->third_column = $request->third_value;

if($model->isDirty()){
// the model has been edited, else codes here will not be executed
}

$model->save();
kapitan
  • 2,008
  • 3
  • 20
  • 26