What does isDirty() mean in Laravel?

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.


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

Tags:

Php

Laravel