Convert raw query to Laravel eloquent

前端 未结 2 422
爱一瞬间的悲伤
爱一瞬间的悲伤 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 19:51

    First link

    Second link

    Query Builder

    DB::table("country")
    ->join('city', 'city.country_code', '=', 'country.user_id')
    ->join('address', 'address.city_id', '=', 'city.id')
    ->select('country.name as country')
    ->where('address.id', 1)
    ->get();
    

    Eloquent

    Country::with(['city','address' => function($query){
        return $query->where('id', 1)
    }])
    ->select('country.name as country')
    ->get();
    

提交回复
热议问题