0

I would like to convert the following sql statement into a Laravel Query Build:

SELECT 
  * 
FROM 
  view_orderline_manifest_data 
where 
  OrderID = 7 
  and (
    ProdCategoryParentID != 4 
    or ProdCategoryParentID IS NOT NULL
  )

Tried the following:

$orderlinedata=DB::table('view_orderline_data')
       ->select('ProdName','ProdID')->where('CustID',$CustID)
       ->where('OrderID',$OrderID)
       ->where('ProdCategoryParentID','!=' , 4)
       ->orWhereNull('ProdCategoryParentID')
       ->pluck('ProdName','ProdID')->all();

The problem is the following is being executed:

SELECT 
  * 
FROM 
  view_orderline_manifest_data 
where 
  OrderID = 7 
  and ProdCategoryParentID != 4 
  or ProdCategoryParentID IS NOT NULL

essentially the brackets () are not being applied.

2 Answers 2

2

To get the result you desire, you'd need to pass a closure to the second where in your code.

$orderlinedata=DB::table('view_orderline_data')
       ->select('ProdName','ProdID')->where('CustID',$CustID)
       ->where('OrderID',$OrderID)
       ->where(function ($query) {
           $query->where('ProdCategoryParentID','!=' , 4)
                 ->orWhereNull('ProdCategoryParentID');
       })
       ->pluck('ProdName','ProdID')->all();

Laravel documentation on Parameter Grouping with the query builder

I hope it helps!

Sign up to request clarification or add additional context in comments.

Comments

0
$orderlinedata = ViewOrderlineData::where('CustID',$CustID)
                                  ->where('OrderID', $OrderID)
                                  ->where(function ($query){
                                          $query->where('ProdCategoryParentID', '!=', 4)
                                                  ->orWhereNull('ProdCategoryParentID');
                                      })->pluck('ProdName','ProdID');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.