Check changes with isDirty() & getChanges() in Laravel Eloquent Model
Tags: laravel, php, webdev, beginners
Published: 2023-12-20 05:45:53

Before diving into `isDirty()` & `getChanges()`, let's have some brief about Laravel Eloquent Model.

## Laravel Eloquent Model
Laravel includes Eloquent, an object-relational mapper (ORM) that makes it enjoyable to interact with a database. When using Eloquent, each database table has a corresponding "Model" that is used to interact with that table. In addition to retrieving records from the database table, Eloquent models allow you to insert, update, and delete records from the table as well. To see more, checkout [Laravel Eloquent](https://laravel.com/docs/10.x/eloquent).

## isDirty()
The `isDirty()` is used BEFORE save, to see what attributes were changed between the time when it was retrieved from the database and the time of the call. If there is any change the `isDirty()` method will return `true`.

```
/**
* Determine if the model or any of the given attribute(s) have been modified.
*
* @param  array|string|null  $attributes
* @return bool
*/
public function isDirty($attributes = null)
{
    return $this->hasChanges(
       $this->getDirty(), is_array($attributes) ? $attributes : func_get_args()
    );
}

/**
* Determine if any of the given attributes were changed.
*
* @param  array  $changes
* @param  array|string|null  $attributes
* @return bool
*/
protected function hasChanges($changes, $attributes = null) {
   // If no specific attributes were provided, we will just see if the dirty array
   // already contains any attributes. If it does we will just return that this
   // count is greater than zero. Else, we need to check specific attributes.
   if (empty($attributes)) {
       return count($changes) > 0;
   }

   // Here we will spin through every attribute and see if this is in the array of
   // dirty attributes. If it is, we will return true and if we make it through
   // all of the attributes for the entire array we will return false at end.
   foreach (Arr::wrap($attributes) as $attribute) {
       if (array_key_exists($attribute, $changes)) {
           return true;
       }
   }


   return false;
}
```
## getChanges()
The `getChanges()` is used AFTER save, to see that attributes were changed/updated in the last save. Eloquent models have two protected arrays, `$original` and `$changes`, which contain the attributes as they were when fetched from storage and the attrbirutes which have been modified, respectively. The `getChanges()` method returns the modified changes.

```
/**
* Get the attributes that were changed.
*
* @return array
 */
public function getChanges()
{
   return $this->changes;
}
```
To get more details checkout [this!](https://github.com/laravel/framework/blob/1bbe5528568555d597582fdbec73e31f8a818dbc/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php#L1066)

## Example
let's have an example of `isDirty()` & `getChanges()`.

```
// Fetch user data from User model
$user = User::findOrFail(1);
$user->name = "Updated Name";
$user->email = "updated@email.com";

// check the model has changed
if ($user->isDirty()) {
    $user->save();
}

// check the model attribute has changed
if ($user->isDirty('email')) {
    $user->save();
}

// wasChanged() is a boolean indicating if the model was modified during the current request lifecycle.
if (!$user->wasChanged()) {
   $changes = $user->getChanges();
}

```

## Conclusion
In summary, isDirty() & getChanges() methods help you to track and manage the changes in Eloquent Model. Xdddddd teda🤓.