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

Wakanda's avatar
Level 10

Construct a siblings relationship for users that share the same parent(USER Model) either mother or father in Laravel Nova

Hi devs, I want to construct a siblings relationship for users that share the same parent(USER Model) either mother or father,

Current Model Setup

/**
     * @return BelongsTo
     */
    public function mother() : BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    /**
     * @return BelongsTo
     */
    public function father() : BelongsTo
    {
        return $this->belongsTo(User::class);
    }

	//  The below correctly returns a user siblings instance collection and it turns out I cannot use a similar method to access this as a User instance siblings collection and it has to be a relationship instead 

	/**
     * @return Collection
     */
    public function siblings()
    {
        if (is_null($this->father_id) && is_null($this->mother_id)) {
            return collect([]);
        }

        return User::where('id', '!=', $this->id)
            ->where(function ($query) {
                if (!is_null($this->father_id)) {
                    $query->where('father_id', $this->father_id);
                }

                if (!is_null($this->mother_id)) {
                    $query->orWhere('mother_id', $this->mother_id);
                }
            })
            ->orderBy('date_of_birth')->get();
    }

The above siblings() method correctly returns a user siblings instance collection and it turns out I cannot use a similar method to access this as a User instance siblings collection In a Laravel Nova resource and it has to be a relationship instead

Table

  			$table->uuid('mother_id')->nullable();
            $table->uuid('father_id')->nullable();

My question in other words is how can I turn the siblings() method into a relationship

0 likes
1 reply
bugsysha's avatar
bugsysha
Best Answer
Level 61

You need a mother and father relationship. You can't cover both relationships with a single one.

1 like

Please or to participate in this conversation.