Eloquent/Laravel: is there a counterpart to object->load('relation'), eg. object->unload('relation')?

Unloading relations can be done by passing an empty array to the model

$child->setRelations([]);

when you call a relation on the model after that, it will be reloaded at that moment.

.. in the current version (5.x) at least, maybe not at the time of your question :)


You can unload relations by unsetting the magic property (at least in Laravel 5.3 and up).

Usage:

unset($model->relation);

What makes this work (from the Model class):

public function __unset($key)
{
    unset($this->attributes[$key], $this->relations[$key]);
}

It does the same as $model->setRelations([]), but for a specific relation (instead of unloading all relations).


As of Laravel 5.6, there is an unsetRelation(string) function.

$parent->load('child');

$parent->unsetRelation('child');

I think this provides a little more readability if you're just trying to unset a single relationship, rather than removing all by $parent->setRelationships([]).