'BelongsToMany single selector in Laravel Nova
I have a Person model assigned to a table in my database (people). A person can have two parents. One male parent and one female parent; both parents,s are also a row in "people." I have a pivot table (person_parent) with the following columns.
- person_id
- parent_id
- gender
The columns' primary key is ["person_id," "gender"] so that the database will enforce one male parent and one female parent. I have a "Parent" relationship defined in my Person model.
public function parents(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
{
return $this->belongsToMany('App/Models/Parent',
'person_parent', 'person_id', 'parent_id');
}
This works great, I can get all parents of relation for eager loading purposes, and I can get the mother and father by doing the following.
public function father()
{
return $this->parents()->wherePivot('gender', 'male');
}
public function mother()
{
return $this->parents()->wherePivot('gender', 'female');
}
And I can set the mother or father in code like this.
$person->mother()->sync($mother);
Now I'd like to present this in Nova to the user in the create or update view for the Person model as two fields. Mother and Father with the same interface as a BelongsTo relationship like this. Is there a way to do this out of the box with Laravel Nova? Or is there a package that can help?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
