Laravel - Getting Started - Examining Attribute Changes

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 the model was originally retrieved.

The isDirty method determines if any of the model's attributes have been changed since the model was retrieved. You may pass a specific attribute name to the isDirty method to determine if a particular attribute is dirty. The isClean will determine if an attribute has remained unchanged since the model was retrieved. This method also accepts an optional attribute argument:

    
    use App\Models\User;
    
    $user = User::create([
        'first_name' => 'Taylor',
        'last_name' => 'Otwell',
        'title' => 'Developer',
    ]);
    
    $user->title = 'Painter';
    
    $user->isDirty(); // true
    $user->isDirty('title'); // true
    $user->isDirty('first_name'); // false
    
    $user->isClean(); // false
    $user->isClean('title'); // false
    $user->isClean('first_name'); // true
    
    $user->save();
    
    $user->isDirty(); // false
    $user->isClean(); // true
	

The wasChanged method determines if any attributes were changed when the model was last saved within the current request cycle. If needed, you may pass an attribute name to see if a particular attribute was changed:

    
    $user = User::create([
        'first_name' => 'Taylor',
        'last_name' => 'Otwell',
        'title' => 'Developer',
    ]);
    
    $user->title = 'Painter';
    
    $user->save();
    
    $user->wasChanged(); // true
    $user->wasChanged('title'); // true
    $user->wasChanged('first_name'); // false
	

The getOriginal method returns an array containing the original attributes of the model regardless of any changes to the model since it was retrieved. If needed, you may pass a specific attribute name to get the original value of a particular attribute:

    
    $user = User::find(1);
    
    $user->name; // John
    $user->email; // [email protected]
    
    $user->name = "Jack";
    $user->name; // Jack
    
    $user->getOriginal('name'); // John
    $user->getOriginal(); // Array of original attributes...