Using isDirty() to check Eloquent attribute changes

Submitted by laraning - 2 months ago

You might come up with a situation where you want to check if a given Eloquent attribute was changed compared with its original stored value.

You have an Eloquent model that you retrieved from the database.
Now, you want to understand if a specific attribute changed prior to the update().

You can use:

if($model->isDirty('email'))

This means "if my current eloquent model has the attribute email different from its original value".

Useful in situations where you want to execute a business operation only in case an
attribute was changed.

Hope it helps,
Bruno
www.laraning.com
comments powered by Disqus