gpt4 book ai didi

sql - 将原始查询转换为 Laravel eloquent

转载 作者:行者123 更新时间:2023-12-02 18:00:22 24 4
gpt4 key购买 nike

如何将此原始查询转换为 Laravel Eloquent 方式:

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

最佳答案

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();

关于sql - 将原始查询转换为 Laravel eloquent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37293971/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com