我有两个模型User和Child。
class User extends Model {
public function children() {
return $this->belongsToMany('App\Child', 'user_child')->withPivot('relation_id');
}
public function connections() {
// How to get the distinctly aggregate of my children.friends.contacts as a relationship here?
}
}
class Child extends Model {
public function contacts() {
return $this->belongsToMany('App\User', 'user_child')->withPivot('relation_id');
}
public function friends() {
return $this->belongsToMany('App\Child', 'child_connection', 'child_id1', 'child_id2');
}
}
我想热切地加载我称之为“连接”的遥远关系,这是我孩子朋友的联系人(用户)。
$user = User::with('children', 'children.friends', 'connections');
任何想法如何优雅地做到这一点?
谢谢!
答案 0 :(得分:1)
尝试
public function connections() {
return $this->belongsToMany('App\Child', 'user_id')->with('friends.contacts;);
}
OMG真是太棒了!
如果任何有效的奇迹,那么
$user = User::find(1);
$user->connections(); //should bring 'children', 'friends' and 'contacts' in one query.
它应该。
如果您找到答案,请发布。我对此非常感兴趣。