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.
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();
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.
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.
* 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With