Laravel Eloquent ORM "whereHas" through table Laravel Eloquent ORM "whereHas" through table sql sql

Laravel Eloquent ORM "whereHas" through table


You have 3 options using relations:

1 most straightforward solution:

Places::whereHas('contacts',function ($q) use ($city_id){       $q->whereHas('cities', function ($q) use ($city_id){           $q->where('id', $city_id);        });    })->get();

2 the same as above but using this PR: https://github.com/laravel/framework/pull/4954

Places::whereHas('contacts.cities', function ($q) use ($city_id){        $q->where('id', $city_id);       })->get();

3 Using hasManyThrough relation:

// Place modelpublic function cities(){  return $this->hasManyThrough('City', 'Contact');}// thenPlaces::whereHas('cities',function ($q) use ($city_id){   $q->where('cities.id', $city_id);})->get();

edit

Having your schema it's obvious that none of the suggest or your original setup can work.

This is a many-to-many relation which in Eloquent is belongsToMany:

// Places modelpublic function cities(){  return $this->belongsToMany('Cities', 'contacts', 'places_id', 'cities_id')    ->withPivot( .. contacts table fields that you need go here.. );}// Cities modelpublic function places(){  return $this->belongsToMany('Places', 'contacts', 'cities_id', 'places_id')    ->withPivot( .. contacts table fields that you need go here.. );}

Then you can call relations like this:

$city = Cities::first();$city->places; // collection of Places models// contacts data for a single city-place pair$city->places->first()->pivot->open_hours; // or whatever you include in withPivot above

Now, there's another way to setup this, in case you need also Contacts model itself:

// Places modelpublic function contact(){  return $this->hasOne('Contacts', 'places_id');}// Contacts modelpublic function city(){  return $this->belongsTo('Cities', 'cities_id');}public function place(){  return $this->belongsTo('Places', 'places_id');}// Cities modelpublic function contact(){  return $this->hasOne('Contacts', 'cities_id');}

then:

$city = Cities::first();$city->contact; // Contacts model$city->contact->place; // Places model

hasManyThrough won't work here at all


As you know the city id and from this you want to find the corresponding place you can start at the city and work back to the place. To do this you will need to define the inverse of your relationships.

// Add this function to your Cities Modelpublic function contact(){    return $this->belongsTo('Contact');}// Add this function to your Contacts Modelpublic function place(){     return $this->belongsTo('Places');}

Now you can query the City and find Place.

$place = Cities::find($city_id)->contact->place;

EDIT:Added missing return in functions


SELECT * from pemeriksaan_ginekologi_iva where id in(SELECT m1.idFROM pemeriksaan_ginekologi_iva m1 LEFT JOIN pemeriksaan_ginekologi_iva m2 ON (m1.id_klien = m2.id_klien AND m1.id < m2.id)WHERE m2.id IS NULL) and id_klien in (select id from klien where id_kelurahan =1)AND (hasil_periksa='IVA Negatif / Normal')  traslate to laravel