Use isDirty(), wasChanged(), and getOriginal() to inspect modified attributes, verify column changes, and retrieve pre-update database values.
When building model observers or audit logging systems, determining whether specific attributes were modified (such as detecting an email or subscription tier change) requires comparing pre-save and post-save model states.
Eloquent provides state inspection methods on every model.
Before Saving: isDirty() and getOriginal()
$user = User::find(1);
$user->email = '[email protected]';
// Check if any attributes or specific attributes were changed
if ($user->isDirty('email')) {
// Retrieve previous value from database
$oldEmail = $user->getOriginal('email');
logger()->info("User {$user->id} changing email from {$oldEmail} to {$user->email}");
}
$user->save();
After Saving: wasChanged()
Inside model saved observers or after $model->save():
if ($user->wasChanged('email')) {
// Send verification email to the new address
$user->sendEmailVerificationNotification();
}
Summary
isDirty('col'): Checks if attributes are modified in memory before saving.getOriginal('col'): Retrieves the unmodified attribute value as fetched from SQL.wasChanged('col'): Verifies if attributes were changed in the most recent save operation.
Related Tips
View all tips →Scope Pivot Table Queries with Closures in wherePivot() in Laravel 13.26
Laravel 13.26 allows wherePivot() and orWherePivot() to accept closures, enabling direct reuse of local scopes defined on custom Pivot models.
Simplify Chronological Query Sorting with latest() and oldest()
Replace verbose orderBy('created_at', 'desc') clauses with the clean latest() and oldest() query builder helpers.