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

Support for ManyToMany? #7

Closed
NicksonYap opened this issue Oct 19, 2018 · 5 comments
Closed

Support for ManyToMany? #7

NicksonYap opened this issue Oct 19, 2018 · 5 comments

Comments

@NicksonYap
Copy link

Given the relationship:
Country → has many to many → User → has many to many → Post

Can we get: all Posts of all Users in a Country

@staudenmeir
Copy link
Owner

Take a look at the documentation:
https://github.com/staudenmeir/eloquent-has-many-deep#belongstomany

@NicksonYap
Copy link
Author

NicksonYap commented Oct 19, 2018

@staudenmeir Thanks!

However, I'm using belongsToMany then belongsToMany again (two pivot tables)
Plus I had to input my foreign and local keys
so it got really confusing

It took me a while to understand the pattern but got it to work, I'm so happy!

Below's an example code, untested with the exact Model & Pivot Tables but should work
(if I mapped the keys and tables correctly)

Example:
User → belongs to many → Role → belongs to many → Permission

class User extends Model
{
    use \Staudenmeir\EloquentHasManyDeep\HasRelationships;

    public function permissions()
    {
        return $this->hasManyDeep(
            'App\Permission',
            ['role_user', 'permission_role'], // Intermediate models and tables, beginning at the far parent (User).
            [           
               'user_id', // Foreign key on the "role_user" table.
               'role_id',      // Foreign key on the "permission_role" table
               'id'  // Foreign key on the "permissions" table. (local key)
            ],
            [          
              'id',         // Local key on the "users" table.
              'role_id', // Local key on the "role_user" table (foreign key).
              'permission_id   // Local key on the "permission_role" table.
            ]
        );
    }
}

@NicksonYap
Copy link
Author

@staudenmeir

Honestly, this should be integrated into Laravel, consider submitting a PR? :)

It took me two days, trying all sorts of ways.
as I've searched for a lot of outdated solutions
(this seems like an issue ever since Laravel was out in 2014)


Note:

I think the readme should be updated with some mentions of the keyword:
HasManyThrough and Many-To-Many
For it to be more SEO friendly (I only found this repo is some 2014 thread in Laravel.io)

@staudenmeir
Copy link
Owner

You don't have to specify the keys when you add the Role model between the pivot tables:

class User extends Model
{
    use \Staudenmeir\EloquentHasManyDeep\HasRelationships;

    public function permissions()
    {
        return $this->hasManyDeep(
            'App\Permission',
            ['role_user', 'App\Role', 'permission_role']
        );
    }
}

@NicksonYap
Copy link
Author

Thanks!

No doubt it will work if the column naming is according to the model name,

but the names of my keys are not following the model name so i Have to do it the manual way like in previous comment: #7 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants