Many to many Laravel Eloquent relationship with multiple intermediate tables

madz

I'm using Laravel 5.4 and have a model and table structure as follows:

users
-----
id

accounts
--------
id

holdings
--------
id
account_id (foreign key to account)

user_accounts
-------------
id
user_id
account_id
  • A user can have multiple accounts
  • An account can be shared by multiple users
  • Each account has multiple holdings
  • A user therefore indirectly has many holdings through their many accounts.

I need help to define a relation on the User model called "holdings" to get me all the holdings applicable to the user (based on the accounts they are linked to).

I've tried lots of different things and spent ages on google. I can get close with both belongsToMany and hasManyThrough, but they only seem to work for 3 table relationships where the intermediate table stores primary keys from the other tables. I can reduce my relationship to 3 tables (rather than 4) if I make use of the account_id foreign key on the holdings table to remove the need to join through the accounts table, however I can't seem to get this to work.

belongsToMany - holdings.id needs to be holdings.account_id:

select * from `holdings`
inner join `user_accounts` on `holdings`.`id` = `user_accounts`.`account_id`
where `user_accounts`.`user_id` = ?

hasManyThrough - user_accounts.id needs to be user_accounts.account_id:

select * from `holdings`
inner join `user_accounts` on `user_accounts`.`id` = `holdings`.`account_id`
where `user_accounts`.`user_id` = ?"
madz

Well it's not strictly an answer as I was asking about laravel 5.4, but it is a possible solution.

It turns out I'm in luck as I came across this Laravel belongsToMany relationship defining local key on both tables which indicates the belongsToMany relationship has been enhanced in Laravel 5.5 to support this type of scenario.

I have upgraded my project to L5.5 and replaced my hack with the relation:

return $this->belongsToMany(Holding::class, 'user_accounts', 'user_id', 'account_id', null, 'account_id');

And am happy to say it seems to work perfectly - at least for basic gets which is my use case, I haven't tried any updates etc.

Thanks to @cyberfly and those who posted the answer above

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Laravel Eloquent many to many filter on intermediate table

From Dev

Laravel eloquent one to many relationship in custom pivot tables

From Dev

Laravel many to many relationship using Eloquent

From Dev

Laravel 5 Eloquent count many to many relationship

From Dev

Many to Many Relationship over multiple tables

From Dev

Many to Many Relationship over multiple tables

From Dev

Laravel - Eloquent relation - many-to-many - get intermediate table columns

From Dev

laravel update, remove one to many relationship eloquent

From Dev

Laravel eloquent update one-many relationship

From Dev

Laravel 5 Eloquent Relationship - Has Many Though

From Dev

Laravel eloquent update one-many relationship

From Dev

Laravel 5 one to many eloquent relationship

From Dev

Laravel 5 Eloquent Relationship - Has Many Though

From Dev

One to Many to One relationship in laravel eloquent

From Dev

eloquent: query many to many relationship

From Dev

Laravel save many-to-many relationship in Eloquent mutators

From Dev

Laravel Eloquent: How to select not attached records in many to many relationship?

From Dev

Need help to structure Eloquent Many to Many relationship (laravel 5.1)

From Dev

Laravel 5.1 Eloquent distant relationship with many to many models

From Dev

Laravel Eloquent Many to Many relationship using a pivot table

From Dev

Laravel 5.2 Eloquent - Model through Many-To-Many Relationship

From Dev

Laravel Eloquent many-to-many relationship: Use explicit pivot table

From Dev

Laravel - Many to Many relationship

From Dev

Many to many relationship in Laravel

From Dev

Many to Many relationship with Laravel

From Dev

Joining multiple tables with one to many relationship

From Dev

Laravel Eloquent Relationship - Inserting data into multiple tables

From Dev

Naming convention for join tables, in a many to many relationship, in Laravel

From Dev

EF6 Many to Many relationship, multiple tables to join table

Related Related

  1. 1

    Laravel Eloquent many to many filter on intermediate table

  2. 2

    Laravel eloquent one to many relationship in custom pivot tables

  3. 3

    Laravel many to many relationship using Eloquent

  4. 4

    Laravel 5 Eloquent count many to many relationship

  5. 5

    Many to Many Relationship over multiple tables

  6. 6

    Many to Many Relationship over multiple tables

  7. 7

    Laravel - Eloquent relation - many-to-many - get intermediate table columns

  8. 8

    laravel update, remove one to many relationship eloquent

  9. 9

    Laravel eloquent update one-many relationship

  10. 10

    Laravel 5 Eloquent Relationship - Has Many Though

  11. 11

    Laravel eloquent update one-many relationship

  12. 12

    Laravel 5 one to many eloquent relationship

  13. 13

    Laravel 5 Eloquent Relationship - Has Many Though

  14. 14

    One to Many to One relationship in laravel eloquent

  15. 15

    eloquent: query many to many relationship

  16. 16

    Laravel save many-to-many relationship in Eloquent mutators

  17. 17

    Laravel Eloquent: How to select not attached records in many to many relationship?

  18. 18

    Need help to structure Eloquent Many to Many relationship (laravel 5.1)

  19. 19

    Laravel 5.1 Eloquent distant relationship with many to many models

  20. 20

    Laravel Eloquent Many to Many relationship using a pivot table

  21. 21

    Laravel 5.2 Eloquent - Model through Many-To-Many Relationship

  22. 22

    Laravel Eloquent many-to-many relationship: Use explicit pivot table

  23. 23

    Laravel - Many to Many relationship

  24. 24

    Many to many relationship in Laravel

  25. 25

    Many to Many relationship with Laravel

  26. 26

    Joining multiple tables with one to many relationship

  27. 27

    Laravel Eloquent Relationship - Inserting data into multiple tables

  28. 28

    Naming convention for join tables, in a many to many relationship, in Laravel

  29. 29

    EF6 Many to Many relationship, multiple tables to join table

HotTag

Archive