3

I have a query that fetches users based on some parameters.

$users = User::where(function($query) use($queryTags_userIDs) {
    $query->whereIn('id', $queryTags_userIDs);
})->orWhere(function($query) use($queryImageTitles_userIDs) {
    $query->whereIn('id', $queryImageTitles_userIDs);
})->orWhere('username', 'LIKE', "%{$query}%")
    ->orWhere('keywords', 'LIKE', "%{$query}%")     
    ->orWhere('profile_text', 'LIKE', "%{$query}%")
    ->orWhere('tagline', 'LIKE', "%{$query}%")
    ->where('role', 2)
    ->orderBy('last_login', 'desc')
    ->paginate(15);

Now, this whole thing works great.

Except this part:

->where('role', 2)

Instead of getting users where role is 2, it fetches users of EVERY role.

However, it WILL work if I get rid of some orWheres.

If I get rid of these 3 orWheres:

->orWhere('keywords', 'LIKE', "%{$query}%")     
->orWhere('profile_text', 'LIKE', "%{$query}%")
->orWhere('tagline', 'LIKE', "%{$query}%")

then it starts fetching users only with role 2. In fact, these 3 specific orWheres are, for whatever reason, causing the problem

If I reduce the query to only

$users = User::where('username', 'LIKE', "%{$query}%")
    ->orWhere('keywords', 'LIKE', "%{$query}%")     
    ->where('role', 2)
    ->orderBy('last_login', 'desc')
    ->paginate(7);

it STILL gets users from outside of role 2.

However, if it were to switch it to this:

$users = User::where('username', 'LIKE', "%{$query}%")
    ->where('role', 2)
    ->orderBy('last_login', 'desc')
    ->paginate(7);

then all of a sudden it only gets users from role 2.

Why is this happening? I feel like I'm missing something super obvious, because this makes no sense.

EDIT

In response to Bassem El Hachem, I put role = 2 at the beginning of the query. But it still doesn't work. Here is the query after the change:

"select * from users where role = ? and (id in (?, ?, ?, ?, ?, ?, ?)) or (id in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)) or (username LIKE ? or keywords LIKE ? or profile_text LIKE ? or tagline LIKE ?) order by last_login desc"

2
  • Try my new answer. The other WHERE are all ORs or is there another mandatory WHERE?
    – BassMHL
    Sep 27, 2017 at 0:38
  • New answer works.
    – Felix
    Sep 27, 2017 at 0:44

1 Answer 1

6

That's because your query in SQL is this:

WHERE keywords like '%...%' OR profile_text LIKE '%...%' 
OR tagline LIKE '%...%' AND role=2

The AND operator in SQL is like multiplication, it adds a parenthesis which means your query is really:

WHERE keywords like '%...%' OR profile_text LIKE '%...%' 
OR (tagline LIKE '%...%' AND role=2)

So the filter role=2 is being applied to specific rows only (the ones where tagline LIKE '%...%'). For rows where keywords like '%...%' OR profile_text LIKE '%...%', role can be any value, which is not what you want.

What you need to do is apply the filtering WHERE on role directly on user. This also helps with efficiency undoubtably.

$users = User::where('role', 2)
  ->where(function($q) use($queryTags_userIDs,$queryImageTitles_userIDs,$query) {
      $q->whereIn('id', $queryTags_userIDs)
      ->orWhereIn('id', $queryImageTitles_userIDs)
      ->orWhere('username', 'LIKE', "%{$query}%")
      ->orWhere('keywords', 'LIKE', "%{$query}%")     
      ->orWhere('profile_text', 'LIKE', "%{$query}%")
      ->orWhere('tagline', 'LIKE', "%{$query}%");
   })->orderBy('last_login', 'desc')
   ->paginate(15);

Since it's not clear what result you want, I would suggest to view the query in SQL syntax. This can be done by replacing paginate(15) by toSql() and dd() the results.

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.