1 回答
TA贡献1815条经验 获得超10个赞
您可以创建两种不同的关系。
public function maleReproductions(){
return $this->hasMany(Reproduction::class, 'father_id');
}
public function feMaleReproductions(){
return $this->hasMany(Reproduction::class, 'mother_id');
}
现在基于$user你可以附加关系。
$productions = [];
$user = User::where('id',1)->first();
if($user->gender == 'm'){
$productions = $user->maleProductions;
} else {
$productions = $user->feMaleProductions;
}
对于用户集合,附加两者的关系。并根据条件访问特定的内容。
$users = User::with('maleReproductions', 'femaleReproductions')->get();
foreach($users as $user){
if($user->gender == 'm'){
$productions = $user->maleProductions;
} else {
$productions = $user->feMaleProductions;
}
}
- 1 回答
- 0 关注
- 270 浏览
添加回答
举报