Laravel 5雄辩的条款和/或条款 [英] Laravel 5 Eloquent where and or in Clauses

查看:70
本文介绍了Laravel 5雄辩的条款和/或条款的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从具有多个where和/或子句的表中获取结果.

i try to get results from table with multiple where and/or clauses.

我的SQL语句是:

SELECT * FROM tbl
WHERE m__Id = 46
AND
t_Id = 2
AND
(Cab = 2 OR Cab = 4)

我如何通过Laravel Eloquent获得此信息?

How i can get this with Laravel Eloquent?

我在Laravel中的代码是:

My Code in Laravel is:

$BType = CabRes::where('m_Id', '=', '46')
                        ->where('t_Id', '=', '2')
                        ->where('Cab', '2')
                        ->orWhere('Cab', '=', '4')
                        ->get();

推荐答案

使用高级位置:

CabRes::where('m__Id', 46)
      ->where('t_Id', 2)
      ->where(function($q) {
          $q->where('Cab', 2)
            ->orWhere('Cab', 4);
      })
      ->get();

或者,甚至更好的是,使用whereIn():

Or, even better, using whereIn():

CabRes::where('m__Id', 46)
      ->where('t_Id', 2)
      ->whereIn('Cab', $cabIds)
      ->get();

这篇关于Laravel 5雄辩的条款和/或条款的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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