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

[Proposal] Belongs to through #3052

Closed
Nirlah opened this issue Dec 26, 2013 · 16 comments
Closed

[Proposal] Belongs to through #3052

Nirlah opened this issue Dec 26, 2013 · 16 comments

Comments

@Nirlah
Copy link
Contributor

Nirlah commented Dec 26, 2013

The inverse of the hasManyThrough() relationship.

class Post extends Eloquent {

    public function country()
    {
        return $this->belongsToThrough('Country', 'User');
    }

}

Anyone else find it useful but me?

@anlutro
Copy link
Contributor

anlutro commented Dec 27, 2013

Can't you use the same relationship both ways? HasManyThrough is effectively a m:m relationship, after all.

@taylorotwell
Copy link
Member

You would just use the same relationship both ways.

@BenjaminRH
Copy link

Unless I'm missing something entirely obvious, which is quite possible, I don't think it is possible to use the hasManyThrough relationship both ways. This example illustrates:

Schema

Table 1: id, table_2_id, foo
Table 2: id, table_3_id, bar
Table 3: id, baz

Problem

Table 3 can have a hasManyThrough relationship with Table 1. However Table 1 cannot have a hasManyThrough relationship with Table 3 because Table 2 does not have a table_1_id.

@MaazAli
Copy link

MaazAli commented Jun 21, 2014

I'd like to see a belongsToThrough relationship for models that work like the following

entry -> daily -> weekly

entry

  • daily_id

daily

  • weekly_id

so if I want to find the week that entry belongs to, I can do a belongsToThrough using daily as the intermediate table.

@upngo
Copy link

upngo commented Jul 4, 2014

Would also like this relationship. The relationship as it currently stands can't necessarily be used both ways.

My schema
table_a: id
table_b: id table_a_id
table_c: id table_b_id

I can use a hasManyThrough on table_a to get to table_c
but I can't use it to get from table_c to get to table_a

Strictly speaking, in my case from table_c to get to table_a I want a belongsToThrough relationship..

@ifaniqbal
Copy link

How about this schema:

table user:
    id_user

table group:
    id_group

table permission:
    id_permission

table user_group:
    id_user_group
    user_id
    group_id

table group_permission:
    id_group_permission
    group_id
    permission_id

How is the best way to retrieve permissions for one user?
Thanks.

@szainmehdi
Copy link

I also can't see how hasManyThrough can work both ways. Here is my current issue:

stores:
    id

categories:
    id
    store_id

items:
    id
    category_id

In this case,

Store -- > hasMany -- > Category
Category --> hasMany --> Item

Item --> belongsTo --> Category
Category --> belongsTo --> Store

Store :: Category :: Item

Item :: Category :: Store

With Laravel 4.1, we can now use hasManyThrough to skip Category and go straight from Store to Item.

Store :: Item [√]

What we can't do is the inverse:

Item :: Store [X]

likely because categories doesn't have a item_id, which it obviously shouldn't have.

Am I missing something? Or is this intended behavior?

###Edit:
Also, even if it does somehow work, it's not ideal since the definition will return a Collection instead of a single instance, like belongsToThrough would ideally return.

@szainmehdi
Copy link

See this StackOverflow answer providing a workaround:
http://stackoverflow.com/questions/23365905/laravel-hasmanythrough-equivalent-belongsto-relationship-through-another-model

The only issue with this is that eager-loading using this method doesn't work:

$item = Item::with('store')->first();
dd($item->store); // => null

@vladrusu
Copy link

vladrusu commented Aug 5, 2014

+1 for BelongsToThrough

@Nirlah
Copy link
Contributor Author

Nirlah commented Aug 5, 2014

@taylorotwell Maybe you should reconsider it again...

@vladrusu
Copy link

vladrusu commented Aug 5, 2014

@taylorotwell As @szainmehdi said earlier, belongsToThrough wouldn't be quite the same as HasManyThrough, because HasManyThrough is a m:m relationship and returns a collection, but what we need in belongsToThrough is to return a single object, not many.

Consider the following example. Supposing we have a users table... For each user we store the city (via a city_id field), but NOT a country. The country is stored in the cities table. We could of course define a "country_id" (in addition to city_id) as well in Users table, but that would break the DRY principle.

It's clear that each City has one and only one country. So a HasManyThrough relationship would be useless.

class User extends Eloquent {

protected $table = 'users';

public function city()
{
    return $this->belongsTo('Cities', 'city_id');
}

public function country()
{
    return $this->belongsToThrough('Country', 'Cities');
}

}

@szainmehdi
Copy link

Yep, I definitely hope you'll reconsider this proposal, @taylorotwell. It has concrete use-case scenarios and I've found myself needing this relationship twice now in my own projects.

@markmooibroek
Copy link

Would be awesome if you could implement this. I just started with Laravel but already in need of this =)

Currently using this to emulate the belongsToThrough (in User model to lookup connected church)

User m--1 Address m--1 Church

public function church(array $columns = null)
{
    return $this->address()->first(["id", "church_id"])->church()->first($columns);
}

@adonespitogo
Copy link

+1 for belongsToThrough

@d-lit
Copy link

d-lit commented Oct 20, 2014

+1 for belongsToThrough

@laravel laravel locked and limited conversation to collaborators Oct 20, 2014
@GrahamCampbell
Copy link
Member

Discussion can continue on #6161.

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