Convert raw query to Laravel eloquent

前端 未结 2 444
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-21 19:35

How to convert this raw query to Laravel eloquent way:

select c.name as country from country c, address ad, city ci where
ad.id = 1 and city.id = ad.city_id and          


        
2条回答
  •  独厮守ぢ
    2021-01-21 20:15

    I will modify the answer from Andrey Lutscevich eloquent part

    Country::select('country.name as country')->has('city')
      ->whereHas('address', function ($query)
      {
        $query->where('id', 1);
      })
      ->get();
    

    Querying Relationship Existence When accessing the records for a model, you may wish to limit your results based on the existence of a relationship use has in that case

    WhereHas methods put "where" conditions on your has queries

提交回复
热议问题