Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pmatt1988's avatar

BelongsToMany single selector

So I have a Person model assigned to a table in my database (people). A person can have 2 parents, one male parent and one female parent. Both parents 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 1 male parent and 1 female parent.

I have a "Parent" relationship defined on my Person model as follows

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 a 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 be able to present this in Nova to the user in the create or update view for the Person model as 2 fields. Mother and Father with the same interface as a BelongsTo relationship Like this: Alt image

Is there a way to do this out of the box with Nova or is there a package that can help?

0 likes
0 replies

Please or to participate in this conversation.