2017-02-16 6 views
2

私は2つのモデルの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'); 

これをどのようにエレガントに行うには?

ありがとうございました!

答えて

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. 

をすべきであること。

回答が見つかったら、投稿してください。私は本当にこれに興味があります。

+0

私は 'children.friends.contacts'に変更しましたが、それでも正しい結果は得られません。私は私の子供の友達の連絡先だけが欲しい。しかし、努力をいただきありがとうございます! –