laravel many to many with third table

crb

I'm using Laravel and trying to grab info from a foreign relationship of a member of a pivot. ERD is below. I have set up a many to many relationship so that I can grab property restrictions with $property->restriction and the other way around. What I'm struggling with is how I can add in the restriction type description when I grab this info.

$property->restriction gives me the type_id

 array(
     'id' => '1',
     'description' => 'Fish only',
     'restriction_types_id' => '1',
     'pivot' => array(
       'property_id' => '17',
       'restriction_id' => '1'
     )
   )

but I want something like this with the restriction_type description instead of its id.

 array(
     'id' => '1',
     'description' => 'Fish only',
     'restriction_type' => 'Animals',
     'pivot' => array(
       'property_id' => '17',
       'restriction_id' => '1'
     )
   )

My DB tables

╔═════════════╗      ╔════════════════════╗      ╔════════════════════╗
║  property   ║      ║property_restriction║      ║restriction         ║
╟─────────────╢      ╟────────────────────╢      ╟────────────────────╢
║id           ║——————║property_id         ║   ┌──║restriction_id      ║
║description  ║      ║restriction_id      ║───┘  ║desctiption         ║
╚═════════════╝      ╚════════════════════╝   ┌──║restriction_type_id ║
                                              │  ╚════════════════════╝
                                              │
                                              │
                     ╔═══════════════════╗    │
                     ║restriction_type   ║    │
                     ╟───────────────────╢    │
                     ║id                 ║────┘
                     ║description        ║
                     ╚═══════════════════╝
dasper

I believe the way you are currently trying to access the data is through a dynamic property. It may be possible to just type out $property->restriction->restriction_type. You can also try to eager load it in one of the ways below:

$property->with('restriction','restriction.restriction_type')->get();

or

$property
    ->with('restriction')
    ->with('restriction.restriction_type')
    ->get();

If neither of these ways work I would research eager loading and the dynamic properties of Laravel and see if there is something that needs to get altered in your relationships.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Hibernate Many to Many without third table

From Dev

One-To-Many Association without a third table

From Dev

Laravel 5 Many to Many - Table name in singular

From Dev

Laravel Eloquent many to many filter on intermediate table

From Dev

Laravel: many-to-many with shared table

From Dev

Laravel 4 - Many to Many - pivot table not updating

From Dev

Laravel Many-to-Many Pivot Table

From Dev

Laravel Many to Many get collection of third instance selection

From Dev

Laravel eloquent - Many to Many, select only matching the many table

From Dev

Laravel - many-to-many where the many-to-many table is (part-) polymorph

From Dev

Laravel - Many to Many relationship

From Dev

Laravel - Many To Many

From Dev

Laravel Many to Many relation

From Dev

Many to many relationship in Laravel

From Dev

Many to Many relationship with Laravel

From Dev

Many to Many laravel

From Dev

Many to many relationship in laravel need a specific name for the table?

From Dev

Update a column that exist in the many to many relationship table in laravel 5.2

From Dev

Laravel: belongsToMany() does not get fields in the many-to-many table

From Dev

Laravel Database Many to Many (Mysql) or keep Json in table

From Dev

Laravel 5 Eloquent Many to Many 2ndary table

From Dev

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

From Dev

laravel 4 Cannot Retrieve ALL results ? pivot table Many to Many

From Dev

Laravel Eloquent Many to Many relationship using a pivot table

From Dev

Laravel belongsToMany inserting '0' on pivot table ids in a many to many relationship?

From Dev

Laravel - Order by pivot value in Many to Many table relationship

From Dev

laravel 5.2 many to many relation retrieve data with intermediate table

From Dev

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

From Dev

Unable to sync conditional pivot table many to many relationship laravel

Related Related

  1. 1

    Hibernate Many to Many without third table

  2. 2

    One-To-Many Association without a third table

  3. 3

    Laravel 5 Many to Many - Table name in singular

  4. 4

    Laravel Eloquent many to many filter on intermediate table

  5. 5

    Laravel: many-to-many with shared table

  6. 6

    Laravel 4 - Many to Many - pivot table not updating

  7. 7

    Laravel Many-to-Many Pivot Table

  8. 8

    Laravel Many to Many get collection of third instance selection

  9. 9

    Laravel eloquent - Many to Many, select only matching the many table

  10. 10

    Laravel - many-to-many where the many-to-many table is (part-) polymorph

  11. 11

    Laravel - Many to Many relationship

  12. 12

    Laravel - Many To Many

  13. 13

    Laravel Many to Many relation

  14. 14

    Many to many relationship in Laravel

  15. 15

    Many to Many relationship with Laravel

  16. 16

    Many to Many laravel

  17. 17

    Many to many relationship in laravel need a specific name for the table?

  18. 18

    Update a column that exist in the many to many relationship table in laravel 5.2

  19. 19

    Laravel: belongsToMany() does not get fields in the many-to-many table

  20. 20

    Laravel Database Many to Many (Mysql) or keep Json in table

  21. 21

    Laravel 5 Eloquent Many to Many 2ndary table

  22. 22

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

  23. 23

    laravel 4 Cannot Retrieve ALL results ? pivot table Many to Many

  24. 24

    Laravel Eloquent Many to Many relationship using a pivot table

  25. 25

    Laravel belongsToMany inserting '0' on pivot table ids in a many to many relationship?

  26. 26

    Laravel - Order by pivot value in Many to Many table relationship

  27. 27

    laravel 5.2 many to many relation retrieve data with intermediate table

  28. 28

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

  29. 29

    Unable to sync conditional pivot table many to many relationship laravel

HotTag

Archive