laravel fill or update code example
Example: laravel fill or update
<?php
$user = User::find(1);
// This will update immediately
$user->update(['first_name' => 'Braj', 'last_name' => 'Mohan']);
//But what if you do not want to update immediately. Suppose you also want to make user active before the actual update. Then fill method comes handy.
$user = User::find(1);
// This will not update underlying data store immediately
$user->fill(['first_name' => 'Braj', 'last_name' => 'Mohan']);
// At this point user object is still only in memory with updated values but actual update query is not performed.
// so we can have more logic here
$user->is_active = true;
// Then finally we can save it.
$user->save(); // This will also make user active
//Update method is dumb and makes the query to database even if no values are changed. But save method is intelligent and calculates if you really changed anything and do not perform query if nothing has changed for example.
$user = User::find(1);
//suppose user object has following values after fetching
// first_name = 'Braj';
// second_name = 'Mohan';
// is_active = true;
// Then if you set the values like following without any change
$user->is_active = true; // it is already true so no change
$user->save(); // this won't perform any database query hence is efficient