There are a few helpful Eloquent functions that help you to know whether the Eloquent object was changed recently or during the request of Laravel.
So there is firstOrCreate()
, which tries to find the first record by email or creates that with name and password.
use App\Models\User; class HomeController extends Controller{ public function index() { $user = User::firstOrCreate( ['email' => 'admin@admin.com'], ['name' => 'Admin', 'password' => bcrypt('password')] ); dump($user->wasRecentlyCreated ? 'Created' : 'Found'); }}
And there's wasRecentlyCreated
. This is a property, not a function.
And if you launch that code on an empty database the first time, it will say created because it created the object, but if you relaunch it the second time, it will say found because it didn't create the object during that second request.
The second helpful method is isDirty()
. If you change any Eloquent method property during the request before saving, you will launch user save at some point, but in the meantime, you've changed some properties.
Then, you can check whether some property or all properties were changed.
use App\Models\User; class HomeController extends Controller{ public function index() { $user = User::firstOrCreate( ['email' => 'admin@admin.com'], ['name' => 'Admin', 'password' => bcrypt('password')] ); dump($user->wasRecentlyCreated ? 'Created' : 'Found'); $user->name = 'Admin updated'; dump($user->isDirty() ? 'Edited' : 'Unedited'); dump($user->isDirty('name') ? 'Name edited' : 'Name not edited'); dump($user->isDirty('email') ? 'Email Edited' : 'Email not edited'); }}
So, at the start, we have a name as "admin", and then we change the name. If you try to launch this code, the general isDirty()
should return "Edited".
With the name field, it should return "Name edited", but with the email field, it should return "Email not edited".
The isDirty()
method works before you save the data into the database.
If you want to check whether the object was changed and saved to the database, then there's a separate method wasChanged()
.
If you want to check whether the database has the newer data, you use wasChanged()
with the same logic, either without parameters or with a parameter of a specific column.
use App\Models\User; class HomeController extends Controller{ public function index() { $user = User::firstOrCreate( ['email' => 'admin@admin.com'], ['name' => 'Admin', 'password' => bcrypt('password')] ); dump($user->wasRecentlyCreated ? 'Created' : 'Found'); $user->name = 'Admin updated'; dump($user->isDirty() ? 'Edited' : 'Unedited'); dump($user->isDirty('name') ? 'Name edited' : 'Name not edited'); dump($user->isDirty('email') ? 'Email edited' : 'Email not edited'); dump($user->wasChanged() ? 'Changed' : 'Unchanged'); $user->save(); dump($user->wasChanged() ? 'Changed' : 'Unchanged'); dump($user->wasChanged('name') ? 'Name changed' : 'Name not changed'); dump($user->wasChanged('email') ? 'Email changed' : 'Email not changed'); }}
Now, if you launch the code, at first, you will see it unchanged before the save call.
Then, when the save is called, you will see the change becomes true.
The name change becomes true
because the name was changed, but the email wasn't changed.
So these are the methods to follow on whether the object was updated or created recently.