Development

In Laravel what does the isDirty method do?

In Laravel the isDirty method can be used to examine the state of your model and its data to determine if changes have been made and need updating.

isDirty will determine if any of the model’s attributes have changed since it was loaded. The best part about it is you can check the whole model or specify attributes.

In the example below you can see that isDirty with no parameters will return true because the title got changed.

It will return true if we use the attributes as the parameter however it returns false for the other attributes that did not get changed.

$post->title = 'Example post title';

$post->isDirty();//true
$post->isDirty('title');//true
$post->isDirty('slug');//false
$post->isDirty('created_at');//false

Note that isDirty can also accept multiple attributes:

$post->isDirty(['title', 'slug']);//true

 

Share

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

1 year ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

1 year ago

FTP getting array of file details such as size using PHP

Using FTP and PHP to get an array of file details such as size and…

2 years ago

Creating Laravel form requests

Creating and using Laravel form requests to create cleaner code, separation and reusability for your…

2 years ago

Improving the default Laravel login and register views

Improving the default Laravel login and register views in such a simple manner but making…

2 years ago

Laravel validation for checking if value exists in the database

Laravel validation for checking if a field value exists in the database. The validation rule…

2 years ago