我有三个表-主席,职务,成员。
MY的要求是将成员映射到董事长并分配一个角色。
当我在members表中有董事长ID和指定ID时,我可以提取董事长下的成员列表。
自主席更换以来,大多数成员保持完整。因此,我想到了将它们编入索引的想法
表-成员映射
id |主席ID | designation_id | member_id
因此,该列表保留有多少位主席来回。我不需要为新主席创建新的个人资料,而不必映射到它。
我现在确定该怎么做,
到目前为止,我已经能够提取ID,但不确定如何加入表格
表格
Chairman
id| name
Designation
id|designation
Members
id|members
这是我的控制人
$mapmember = Statechairman::findOrFail($id)->statechairmembersmap;
dd($mapmember);
在此Iam中,获取了statechairmembersmap,但它正在获取所有结果,而不是限制匹配。
我还尝试使用数据库加入查询
$mapmember = DB::table('statechairmen')
->join('state_chairman_members_maps', 'state_chairman_members_maps.chairman_id','statechairmen.id')
->join('statemembers','statemembers.id','state_chairman_members_maps.members_id')
->select('state_chairman_members_maps.*')->get();
but this result show me the Table - membermap but not the other results.
我的模特:
主席:
public function statechairmembersmap(){
return $this->hasMany('App\StateChairmanMembersMap','chairman_id','id');
}
public function statemembers(){
return $this->hasMany('App\Statemembers','chairman_id', 'id');
}
public function statedesignation(){
return $this->hasMany('App\Statedesignation','id','designation_id');
}
会员地图:
protected $table = 'state_chairman_members_maps';
protected $dates = ['deleted_at'];
public function statechairman(){
return $this->belongsTo('App\Statechairman','id');
}
public function statedesignations(){
return $this->belongsTo('App\Statedesignation','designation_id','id');
}
public function statemembers(){
return $this->belongsTo('App\Statemembers','members_id','id');
}
如果我做错了什么,请帮助我。
答案 0 :(得分:0)
非常感谢您解决问题。 最终,经过很多努力,我自己找到了它。
$mapmembers = DB::table('state_chairman_members_maps')
->join('statechairmen','statechairmen.id','=','state_chairman_members_maps.chairman_id')
->join('statemembers','statemembers.id','=','state_chairman_members_maps.members_id')
->join('statedesignations','statedesignations.id','=','state_chairman_members_maps.designation_id')
->where('chairman_id','=',$id)
->get();
这是我想出的。
在这里,我加入了3个表并映射了主席的ID,以过滤结果。我得到结果。