これらは私のテーブルです:laravelの1つのテーブル内で親子関係を取得する最も良い方法は?
ユーザー:
id username password
1 user1 ********
2 user2 ********
3 user3 ********
4 user4 ********
5 user5 ********
6 user6 ********
UserConnections:
pivot_id parent_user_id child_user_id
1 1 2
2 1 3
3 3 4
4 3 5
class User extends Model
{
public function connections()
{
return $this->hasMany('App\UserConnections')->with(['parent','child']);
}
}
class UserConnection extends Model
{
public function parent()
{
$this->belongsTo('App\User','parent_user_id');
}
public function child()
{
$this->belongsTo('App\User','child_user_id');
}
}
$user = App\User::find(3);
今、私は、ユーザーID 3の$user->connections
を呼び出すとき、
それは
UserConnections
テーブルから私のexplaination 2,3 & 4行を与えるべきである:第二行で
を、USER3行でUSER1
ある親を有する第&第四、USER3は、二つを有しています子供
。 接続テーブル - id、タイプ(例:{[1 => 'parent'、2 => 'child']}) UserConnectionには誰が誰とどのような接続タイプを持っていますか。 –
@Autista_zあなたのソリューションから、私は1つの接続のために2つの行を作成しなければなりません、親と子供のための1つです。 –
それを考えた後、私はあなたの解決策が最高だと分かった@Autista_z助けてくれてありがとう。 –