Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 7.x: Difference between fresh and refresh method?

While I was reading Laravel documentation, I faced a method named fresh and refresh on the Eloquent model. Please explain the major difference between them? I am having hard time understanding those.

like image 907
Gursewak Singh Avatar asked Sep 16 '20 11:09

Gursewak Singh


People also ask

What is fresh () in laravel?

Laravel 5 is providing a "fresh()" method that will return a new instance of the current model. Once you're using Laravel 5.0 or newer, you can reload a model like this: $model = $model->fresh();

What is migrate fresh?

php artisan migrate:fresh is used when we want a fresh or new installation of our database. It deletes all the existing tables of the database and runs the migrate command.

What does migrate refresh do?

You may be familiar with the migrate:refresh command that allows you to rollback and re-runs all of your migrations. This helps when you need to rebuild your database during development.

How do I refresh a specific migration?

* To run a specific migration php artisan migrate:refresh --path=/database/migrations/2019_03_23_165757_create_combined_1553343771_users_table. php - Note: it will drop the table and create a new one.


1 Answers

This is the comment for the refresh method on Illuminate\Database\Eloquent\Model :

/**
 * Reload the current model instance with fresh attributes from the database.
 *
 * @return $this
 */
public function refresh()

Here is the one for fresh:

/**
 * Reload a fresh model instance from the database.
 *
 * @param  array|string  $with
 * @return static|null
 */
public function fresh($with = [])

refresh will refresh the current model instance (including relationships). fresh will get a new instance of the model from the database and return it.

like image 96
lagbox Avatar answered Oct 02 '22 06:10

lagbox