I have a problem while doing some SELECT
query for making a "Search" method.
Here is my table structure
table 'users':
name | varchar,
username | varchar,
role | enum[Admin, Owner, Cashier],
address | text
I have made an ajax script that sends a parameter called query
into my Laravel Controller
. This query
parameter contains input from the users searching for some data in my admin.blade.php
page that only shows users having Admin
role from the users' table. Here is my query:
$data = DB::table('users')
->join('outlets','users.outlet_id','outlets.outlet_id')
->where('users.role','Admin')
->where('users.nama','like','%'.$query.'%')
->where('users.username','like','%'.$query.'%')
->orWhere('outlets.nama_outlet','like','%'.$query.'%')
->orWhere('users.address','like','%'.$query.'%')
->orderBy('users.user_id')
->get();
You don't have to give attention to the join
. The point of my problem is the query above returns also the user that has Owner
and Cashier
role. For example, I have an Admin
named John
and an Owner
named Jane
, and they both have the same address, like New York
. When the user typed New York
in the search box, the query above should return only John
. But, in my case, it also returns Jane
.
How must I do the query?
I hope you understand my question. Thank you.