Tuesday, October 21, 2025
HomeLaravelLaravel Tip: $model->refresh() vs $model->fresh()

Laravel Tip: $model->refresh() vs $model->fresh()

💡 Laravel Tip: $model->fresh() vs $model->refresh()

These two Eloquent methods don’t get much attention — but they can save you from subtle bugs when working with updated models.

Imagine you have a $user model:

$user = User::find(1);
$user->update(['email' => 'new@email.com']);

Now, if you immediately check $user->email, you’ll still see the old value!
That’s because update() writes to the database, but doesn’t refresh your in-memory object.

✅ To fix this:

$user->refresh(); // reloads the same instance from DB
echo $user->email; // now shows the updated email

And if you prefer a new instance instead of modifying the current one:

$freshUser = $user->fresh();
  • $model->refresh() → updates the current object with latest DB data
  • $model->fresh() → returns a new instance with latest DB data

Small details like this make your code more predictable and bug-free. 🚀

#Laravel #Eloquent #PHP #WebDevelopment #CleanCode

Atiq
Atiqhttps://elearn.softscholar.com
Software Engineer with extensive experience developing modern web applications using various programming languages and frameworks. Strong background in converting legacy systems to modern architectures and contributing to SaaS platforms. Proficient in working with microservices and cloud technologies. Adept at managing teams, implementing CI/CD pipelines, and deploying SaaS platforms to optimize enterprise operations. Proven ability to lead cross-functional teams and mentor junior developers, ensuring project success and team growth.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments