In laravel, after using attach() or detach() to add or remove something from a relation, the collection has not changed. So if I have a model whose realation contains
[1, 2], after this:
it will still contain
[1, 2]! How do I refresh it?
You can easily tell laravel to do it with a single command:
Will tell it to refresh the relation collection, and
$model->relation will now show the correct values.
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 = ;
Conclusion: three solutions in here
$model->load('relation'); unset($model->relation); $freshCollection = $user->roles()->get();`
unset it and let the system reload on demand.
And let it be loaded on request.