Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HasOne Through as an Eloquent relationship #8721

Closed
dwightwatson opened this issue May 13, 2015 · 14 comments
Closed

HasOne Through as an Eloquent relationship #8721

dwightwatson opened this issue May 13, 2015 · 14 comments

Comments

@dwightwatson
Copy link
Contributor

I'd like to suggest that we add hasOneThrough() as an additional Eloquent relationship. I'll use the example of the hasManyThrough() relationship to explain the use case. You can use the hasManyThrough() relationship to get the posts of a country. A hasOneThrough() relationship would allow you to get the country of a post.

class Post extends Model
{
    public function country()
    {
        return $this->hasOneThrough('App\Country', 'App\User');
    }
}

This effectively allows inverse transversal of the relationship between the chain of models. This relationship exists in Rails, and I've supplied the SQL statement used by ActiveRecord to fetch the country from a post model.

SELECT  "countries".* FROM "countries" INNER JOIN "users" ON "countries"."id" = "users"."country_id" WHERE "users"."id" = ? LIMIT 1
@ChangePlaces
Copy link

Maybe I'm misunderstanding - why not use a getCountryAttribute method and return $this->user->country?

@dwightwatson
Copy link
Contributor Author

Because that's abusing attributes to work like relations. You can't eager load it, you can't query it and you can't assign it.

@keevitaja
Copy link
Contributor

+1

1 similar comment
@IVIR3zaM
Copy link

IVIR3zaM commented Aug 2, 2015

+1

@djtarazona
Copy link
Contributor

+1

I could definitely use this.

@samuel-cloete
Copy link

+1

@GrahamCampbell GrahamCampbell changed the title [Request] HasOne Through as an Eloquent relationship HasOne Through as an Eloquent relationship Aug 21, 2015
@EricWVGG
Copy link

EricWVGG commented Oct 1, 2015

+1

3 similar comments
@ovp87
Copy link

ovp87 commented Oct 2, 2015

+1

@JapSeyz
Copy link

JapSeyz commented Nov 2, 2015

+1

@brandfocus
Copy link

+1

@GrahamCampbell
Copy link
Member

Please don't 👍 issues. It doesn't really help anyone. Sending a PR would help, adding a +1 does not.

@GrahamCampbell
Copy link
Member

Thanks for the report, but we don't process feature requests. We are, however, open to PRs.

@nejtr0n
Copy link

nejtr0n commented Aug 4, 2016

+1

1 similar comment
@Barbery
Copy link

Barbery commented Sep 28, 2016

+1

@laravel laravel locked and limited conversation to collaborators Sep 28, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests