Appearance
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:
Example of using the refresh method on a model instance to reload the model's attributes from the database in Laravel
php
<?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();
<div class="alert alert-info flex not-prose">Watch a video course Learn object oriented PHP
</div>
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:
Example of using the fresh method to reload the model's attributes from the database in Laravel
php
<?php
$user = User::find(1);
$freshUser = $user->fresh();The refresh() method updates the existing model instance in place, making it useful when you need to continue working with the same object after external database changes. The fresh() method returns a completely new instance, which is preferable when you want to avoid side effects on the original object or need a clean snapshot of the current database state.