laravel - Manually inject model relation using Eloquent -
how can add model relations array of model?
e.g.
- domain belongsto owner.
- owner hasone domain.
- i have $domain (instance of domain).
- i have $owner (instance of owner).
i want add $domain
$owner->relations[]
can use $owner->domain
later on in code.
the reason doing such in 1 particular controller need partial data set each model use fluent query join performance reasons fill models.
then readability's sake i'd use $owner->domain->id
etc
$domain->owner()->associate($owner);
gives me $domain->owner
option
but can't work out opposite version
$owner->domain()->associate($domain) $owner->domain()->attach($domain)
both result in following fatal error
call undefined method illuminate\database\query\builder::[attach|associate] ()
nb: don't want save i've loaded data need.
setrelation()
should work. sets value in relations
array.
$owner->setrelation('domain', $domain);
Comments
Post a Comment