Laravel: Create HasMany Relationship Based On Condition
问题 I need to display reproductions based on Gender Condition Reproduction Table: $table->bigIncrements('id'); $table->unsignedInteger('father_id'); $table->unsignedInteger('mother_id'); ... I need to retrieve reproductions according to the gender User.php public function reproductions(){ if($this->gender == 'm'){ return $this->hasMany(Reproduction::class, 'father_id'); }else{ return $this->hasMany(Reproduction::class, 'mother_id'); } } Reproduction Table: id father_id mother_id 1 1 2 2 1 3 3 1 4