hy_sultani
hy_sultani

Reputation: 27

Laravel how to match 2 values with one field

Currently I am facing problem that I am using orWhere to get the correct result but not getting the exact result. I want to get result where my field contain only Paid and Flexible

E.g Status

But with this query I am also getting UnPaid but I don't want UnPaid result

$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
)

Upvotes: 0

Views: 997

Answers (2)

Martin Bean
Martin Bean

Reputation: 39439

I know you’ve already accepted an answer, but it’s really not the best way. You could use Laravel’s whereIn query method:

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

The first parameter is the column name, and the second is an array of values you want to match.

Upvotes: 2

Muhammad Hamza Younas
Muhammad Hamza Younas

Reputation: 508

Use Db::RAW for more help read: 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);

Upvotes: 0

Related Questions