将原始查询转换为Larvel雄辩 [英] Convert raw query to Larvel eloquent

查看:154
本文介绍了将原始查询转换为Larvel雄辩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助我将这个原始查询转换为Laravel雄辩的方式:

Can anyone help me 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


推荐答案

我将修改 Andrey Lutscevich 雄辩的答案部分

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




查询关系存在
当访问模型的记录时,您可能希望根据在 c $ c> 之间的关系使用

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方法将where条件放在您的查询上

WhereHas methods put "where" conditions on your has queries

这篇关于将原始查询转换为Larvel雄辩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆