In this laravel eloquent tutorial we will discuss use of isDirty, isClean and wasChanged method. isDirty, isClean and wasChanged method determine that any attribute(column) of model change or not.
Laravel eloquent isDirty vs isClean vs wasChanged
- isDirty()
- isClean()
- wasChanged()
Laravel eloquent isDirty() method
isDirty method determines if any model attribute has been changed when it retrieves. We also pass a single attribute and check its status that it changed or not. Thus isDirty method check model instance value changed or not. We use isDirty() method after a model instance then it give false or true value false mean value changed and true mean value not changed.
Let explain with example create a student model and after change some value on this instance and check isDirty value
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; class StudentController extends Controller{ public function index(){ $student = Student::create([ 'name' => 'advanced web', 'age' => '21' ]); $student->age = '30'; $student->isDirty(); // true $student->isDirty('age'); // true $student->isDirty('name'); // false $student->save(); $user->isDirty(); // false } }
In above example first method direct use isDirty() it give true mean something change in model and second is also true mean age value changed and last give false mean name value not changes
Laravel eloquent isClean() method
isClean method is just opposite of isDirty method. In isClean method give true if no value changed of any attribute of model instance and any attribute changed then it gives false. More explain about isClean() method see below example.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; class StudentController extends Controller{ public function index(){ $student = Student::create([ 'name' => 'advanced web', 'age' => '21' ]); $student->age = '30'; $student->isClean(); // false $student->isClean('age'); // false $student->isClean('name'); // true $student->save(); $user->isClean(); // true } }
Laravel eloquent wasChanged() method
wasChanged() method determine after save data any attribute value changed. We also pass a single attribute in wasChanged() method. Example of wasChanged method
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; class StudentController extends Controller{ public function index(){ $student = Student::create([ 'name' => 'advanced web', 'age' => '21' ]); $student->age = '30'; $student->save(); $student->wasChanged(); // true $student->wasChanged('age'); // true $student->wasChanged('name'); // false } }
You read this tutorial on advanced web tutorial. here we provide laravel beginners to advanced tutorial.
Anmol Sharma
I am a software engineer and have experience in web development technologies like php, Laravel, Codeigniter, javascript, jquery, bootstrap and I like to share my deep knowledge by these blogs.
Related tutorial links
- What is laravel eloquent model and introduction
- part 1 laravel eloquent model all configuration hindi
- part 2 laravel insert and create data by eloquent model tutorial
- part 3 laravel data retrieve method
- part 4 first, find, findOrFail and firstOrFail method
- part 5 laravel delete vs truncate method
- part 6 laravel save vs update method
- part 7 laravel updateOrCreate and upsert
- part 8 laravel chunk vs cursor method
- part 9 laravel eloquent subquery select
- part 10 laravel findOrFail vs firstOrFail
- part 11 laravel create and retrieve data
- part 12 laravel eloquent aggregate functions
- part 13 laravel isDirty vs isClean vs wasChanged method