How to reload/refresh model from database in Laravel?
In Laravel, you can use the refresh
method on a model instance to reload the model's attributes from the database. For example, if you have a model called User
:
<?php
$user = User::find(1);
// Update the user's name in the database
DB::table('users')
->where('id', 1)
->update(['name' => 'John Doe']);
// Reload the user's attributes from the database
$user->refresh();
Watch a video course
Learn object oriented PHP
You can also use the find
method with the refresh
option set to true
to reload the model's attributes from the database:
<?php
$user = User::find(1, ['*'], ['refresh' => true]);
Another way to refresh a model is by using the fresh
method, which returns a new instance of the model with the latest data from the database:
<?php
$user = User::find(1);
$freshUser = $user->fresh();
This method returns a new instance of the model with the latest data from the database.