How can I add a model into the relations array of another model?
例如.
- 域名属于所有者.
- 所有者只有一个域名.
- 我有$domain(Domain的实例).
- 我有$Owner(Owner的实例).
我想把$domain
加到$owner->relations[]
,这样我以后就可以在代码中使用$owner->domain
了.
The reason for doing this is such that in one particular controller i only need a partial data set from each model so use fluent to query with a join for performance reasons then fill the models.
那么为了可读性,我想用$owner->domain->id
等.
$domain->owner()->associate($owner);
gives me a $domain->owner
option
But then I can't work out the opposite version
$owner->domain()->associate($domain)
$owner->domain()->attach($domain)
两者都会导致以下致命错误
Call to undefined method Illuminate\Database\Query\Builder::[attach|associate] ()
NB: I don't want to save anything as I've already loaded all the data i need.个