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 ║
╚═══════════════════╝
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.
Comments