Introduction:

Monitoring changes made to model instances is a typical requirement in Laravel applications. Finding out if a model has changed after it was pulled from the database is made easy with Laravel. This blog post will explain how to check if a model in Laravel has been modified.

Description:

The wasChanged method from Laravel allows you to determine whether any model attributes have changed. If an attribute has changed since the model was last saved during the current request cycle, this function returns true; if not, it returns false.

Example:

Here’s how you can use the wasChanged method:
waschanged

Above returns true as we changed the author name.

Method also allows us to check by specific field or array of fields passing as below.
imgcode

While passing with array if any of the fields were changed then it also returned true.
imgcode2

As seen in above code it returns false as it doesn’t change.

Laravel also provides a couple of more methods like isDirty() and isClean() to identify the status of the model.

Support On Demand!

Laravel