-
-
Notifications
You must be signed in to change notification settings - Fork 157
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
Comments
Take a look at the documentation: |
@staudenmeir Thanks! However, I'm using belongsToMany then belongsToMany again (two pivot tables) 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 Example: 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.
]
);
}
}
|
Honestly, this should be integrated into Laravel, consider submitting a PR? :) It took me two days, trying all sorts of ways. Note: I think the readme should be updated with some mentions of the keyword: |
You don't have to specify the keys when you add the class User extends Model
{
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function permissions()
{
return $this->hasManyDeep(
'App\Permission',
['role_user', 'App\Role', 'permission_role']
);
}
} |
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) |
Given the relationship:
Country
→ has many to many →User
→ has many to many →Post
Can we get: all
Posts
of allUsers
in aCountry
The text was updated successfully, but these errors were encountered: