How do I reload a relation collection in laravel?
You can easily tell laravel to load a relation with a single command:
$model->load('relation');
Will tell it to refresh the relation collection, and $model->relation
will now show the correct values.
Also unloading a relation will be like this:
$model->unsetRelation('relation')
either just unset
it and let the system reload on demand.
unset($model->relation)
or
$model->unsetRelation('relation');
And let it be loaded on request.
Conclusion: three solutions in here
$model->load('relation');
unset($model->relation);
$freshCollection = $user->roles()->get();`
It is possible to use Eloquent query builder:
$freshCollection = $user->roles()->get();
If you want to force all your relations to reload on an as-needed basis and you're inside your model, you can use:
$this->relations = [];