Reputation: 3965
I am using laravel 4 query. I want get data by OR condition. Is there any priority rule with Laravel 4 ?
My Query is :
SELECT *
FROM order_items
WHERE (status = 'Y' OR (status = 'N' AND amount > 100))
AND product_id = '15'
LIMIT 1;
I have 2 order items but i want the first item in the result will get the prority status = 'Y', if its not there, then it meet the second condition of OR.
Here is my laravel code :
$sql_select = "SELECT *
FROM order_items
WHERE (status = 'Y' OR (status = 'N' AND amount > 100))
AND product_id = '15'
LIMIT 1";
$data = DB::select($sql_select);
echo "<pre>";
print_r($data);
die;
For simple mysql query, its working, but in laravel its not giving results.
Upvotes: 0
Views: 2804
Reputation: 416
Great solution andreeab.
In my case the SQL query is:
SELECT ... WHERE c1 = 'xyz' AND (c2 = 100 OR c2 = 200);
I wrote in Eloquent:
DB:table('tablename')
->where('c1', '=', 'xyz')
->where(function($query)
{
$query->where('c2', '=', 100)
->orWhere('c2', '=', 200);
})
->get();
Upvotes: 0
Reputation: 81
Maybe you should try this way
DB::table('order_items')
->where('status', 'Y')
->orWhere(function($query)
{
$query->where('status', 'N')
->where('amount', '>', '100');
})
->where('product_id', 15)
->first();
Upvotes: 3