将原始查询转换为 Laravel eloquent [英] Convert raw query to Laravel eloquent
本文介绍了将原始查询转换为 Laravel eloquent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将此原始查询转换为 Laravel 雄辩的方式:
select c.name as country from country c, address ad, city ci wheread.id = 1 and city.id = ad.city_id and c.code = ci.country_code
解决方案
我会修改Andrey Lutscevich
eloquent part
Country::select('country.name as country')->has('city')->whereHas('address', function ($query){$query->where('id', 1);})->get();
<块引用>
查询关系是否存在在访问模型的记录时,您可能希望根据关系的存在限制结果,在这种情况下使用 has
WhereHas 方法将where"条件放在你的查询上
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 c.code = ci.country_code
解决方案
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
这篇关于将原始查询转换为 Laravel eloquent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文