hasManyThrough relationship troubles

Posted 4 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.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.