lundi 25 avril 2016

One to many/inverse relationship - Laravel

This seems simple enough but I can't seem to figure it out.

I have the below Models

City -> HasMany Locations Locations -> HasMany Restaurants so Restaurants -> BelongsTo Locations

This means that Restaurant is linked to City via Locations

Now I want to find Restaurants in a particular City based the Location provided.

The values provided are the City name and Location name. So I can get the city_id and location_id

// this will get a city with all locations
$city = City::with('locations')->where('value', $c)->first();
$city_id = $city->id;

// this will get the location id
$location = Location::where('value', $l)->first();
$location_id = $location->id;

So my $restaurants query should find all restaurants where the location is part of the location of $city.

How can I achieve this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire