0

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?

3
  • Where is search_price < rrp_price' Commented Jun 23, 2017 at 6:40
  • search_price < rrp_price is the last line. They are properties in the table. Commented Jun 23, 2017 at 6:51
  • Ah sorry, it should be != I've been playing around with != and < , but the issue remains on both so doesn't seem dependant on that Commented Jun 23, 2017 at 8:13

4 Answers 4

1

Have you tried <> instead of != ? Maybe it'll work

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

2 Comments

It's the "not equal" operator for SQL.
Ah thanks, doesn't make any difference. Thanks though!
0

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);
   }
}

1 Comment

That makes it a 'and where', rather than a 'or where', right? That part of the query works, it's the price filter that doesn't work.
0

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

1 Comment

They don't seem to work either. It's probably worth mentioning that i'm casting the value to a string (as some of the prices include a GBP suffix) and then stripping them of any currency values, and converting to a float. Also, if i run the query in the original question without the product id filter, the price filter works. Odd
0

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.

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.