hasManyThrough relationship troubles

Posted 8 months ago by xtremer360

I think I may have misinterpreted the documentation for how to use the hasManyThrough relationship. I have the following table structure and I'm trying to get a collection of titles that the wrestler has had. I originally had this as a hasMany relationship but since I have a test that I need to get the collection of the actual titles then I changed it to a hasManyThrough.

Wrestler: id, name
Champion: id, wrestler_id, title_id, won_on, lost_on
Title: id, name

So on Wrestler model I have this.

/**
     * A wrestler can hold many titles.
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function titles()
    {
        return $this->hasManyThrough(Title::class, Champion::class, 'id', 'id', 'id', 'id');
    }

/**
     * Retrieves a collection of titles currently held by wrestler.
     *
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public function currentTitlesHeld()
    {
        return $this->titles()->whereNull('lost_on');
    }

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.