I've got DB tables of Region (list of cities), Rental (list of rental information), and Rental_Location (list of rental addresses + lat/long).
The basic relationships are:
Regions haveMany Rentals (with FK's region_id and rental_location_id in Rentals table)
Locations haveMany Rentals, or the reverse logic Rental belongsTo a Location
What I'd like to do is get the location information of all the rentals in a region from a region_id (from the rentals table), as well as include the location spatial data (rental_locations table):
$region = Region::find(1);
$rentals = $region->rentals; // doesn't contain location information yet
$rentalDataWithSpatialLocationData = $region->rentals->location; // what I liked to do
But the last line using the belongsTo relationship won't work by adding the spatial data from rental_locations into the original rentals collection.
Right now the $rentals collection contains all the rentals for a region, but no spatial data to mark the location on a map, but other than using a join in QueryBuilder something like this below and not using the ORM hasMany properties in my models:
$spatials = DB::table('rentals')
->join('regions', 'rentals.region_id', '=', 'regions.id')
->join('rental_locations', 'rentals.rental_location_id', '=', 'rental_locations.id')
->where('rentals.region_id', '=', $region_id)
->select('*')
->get();
Basically, I can't figure out how to get a collection of all the rentals with their location information, is this possible using the ORM? Seems like this might get expensive compared to a join, so I'm assuming right now you don't use the ORM to the exclusion of the QueryBuilder, but more to supplement simple and quick relationships queries only?
$regeion->rentals
is a collection, so you can't just do this -$region->rentals->location
you should access locations for each rental separately – Danny