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:
$model->relation()->detach(1); $model->relation()->attach(3);
it will still contain [1, 2]
! How do I refresh it?
One To One (Polymorphic) One To Many (Polymorphic) Many To Many (Polymorphic)
In eloquent ORM, $fillable attribute is an array containing all those fields of table which can be filled using mass-assignment. Mass assignment refers to sending an array to the model to directly create a new record in Database.
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')
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