我需要根据性别条件显示复制品
再现表:
$table->bigIncrements('id');
$table->unsignedInteger('father_id');
$table->unsignedInteger('mother_id');
...
我需要根据性别检索复制品
用户.php
public function reproductions(){
if($this->gender == 'm'){
return $this->hasMany(Reproduction::class, 'father_id');
}else{
return $this->hasMany(Reproduction::class, 'mother_id');
}
}
再现表:
id father_id mother_id
1 1 2
2 1 3
3 1 4
当我检索 ID 为 1 的用户的复制品时,它需要显示 3 个复制品,但它返回 null 集合
$firstUser = User::find(1); // User Id with 1 has gender m
dd($firstUser->reproductions->count()); // Should return 3 but returns null collection
动漫人物