Laravel如何将2个值与一个字段匹配

时间:2015-05-13 07:18:18

标签: mysql laravel

目前我遇到的问题是我使用orWhere来获取正确的结果但没有得到确切的结果。我希望得到的结果是我的字段只包含付费灵活

E.g 的状态

  • 付费
  • 付费
  • 弹性
  • 付费
  • 付费
  • 弹性

但是通过这个查询,我也得到了 UnPaid ,但我不想要取消支付结果

$orders = Order::with('orderfiles','orderStatus','orderDelivery','flexibleDelivery')->whereHas('payment', function ($query) {
            $query->where("status","=",'Paid')->orwhere('status' ,'=', 'Flexible');
      })->whereBetween("tbl_orders.order_date",  $range)->where('domain_id','=',$domain_id)->orderBy('order_date','asc')->paginate();
           return View('admin.all_orders')
               ->with('orders',$orders)->with('title','Results - Paid and Flexible Orders')->with('logoNum',$domain_id);

Sql

Array
(
    [0] => select count(*) as aggregate from `tbl_orders` where (select count(*) from `tbl_payments` where `tbl_payments`.`order_id` = `tbl_orders`.`order_id` and `status` = ? or `status` = ?) >= 1 and `tbl_orders`.`order_date` between ? and ? and `domain_id` = ?
    [1] => Array
        (
            [0] => Paid
            [1] => Flexible
            [2] => 2015-01-01
            [3] => 2015-05-31
            [4] => 18
        )

    [2] => 7233.41
    [3] => mysql
)

2 个答案:

答案 0 :(得分:1)

我知道你已经接受了答案,但这并不是最好的方法。您可以使用Laravel的whereIn查询方法:

$query->whereIn('status', ['Paid', 'Flexible']);

第一个参数是列名,第二个是要匹配的值数组。

答案 1 :(得分:0)

使用Db :: RAW 如需更多帮助,请阅读:http://laravel.com/docs/4.2/queries

$orders = Order::with('orderfiles','orderStatus','orderDelivery','flexibleDelivery')->whereHas('payment', function ($query) {
        $query->where(DB::raw('(`status` = \'Paid\') OR (`status` = \'Flexible\')'));
      })->whereBetween("tbl_orders.order_date",  $range)->where('domain_id','=',$domain_id)->orderBy('order_date','asc')->paginate();
           return View('admin.all_orders')
               ->with('orders',$orders)->with('title','Results - Paid and Flexible Orders')->with('logoNum',$domain_id);