I have the following query that seems to go wrong on the second where (search_price < rrp_price)
$query = Product::where('product_name', '!=', 'product_name');
$likes = DB::table('likes')->selectRaw('product_id, COUNT(*) as count')
->groupBy('product_id')
->orderBy('count', 'desc')
->get();
foreach($likes as $like){
if($like == $likes[0]){
$query->where('aw_product_id', $like->product_id');
}
else{
$query->orWhere('aw_product_id', $like->product_id');
}
}
$query->whereRaw('search_price != rrp_price');
$products = $query->get();
So, if i take out the 'search_price < rrp_price' where i get all products filtered by those product ids with no price filtering, but when i leave the 'search_price < rrp_price' filter in, nothing seems to happen (search_price and rrp_price are always the same).
Am i doing anything obvoiously wrong with my where statements?
Have you tried <>
instead of !=
? Maybe it'll work
There is error in your query Modified query is as below :
foreach($likes as $like){
if($like == $likes[0]){
$query->where('aw_product_id', $like->product_id);
}
else{
$query->Where('aw_product_id', $like->product_id);
}
}
Did you try any other where syntax? something like this:
->where('table.search_price', '<>', DB::raw('table.rrp_price'))
Or maybe (Laravel 5+):
->whereColumn('table.search_price','<>','table.rrp_price')
It turns out it was because the values were strings. Even though I was casting 'search_price' and 'rrp_price' to be floats, the actual laravel query seemed to be comparing string values in the database.
I modified the values in the database to be floats (for now) and now the query works.