1

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.

Dani Fadli
  • 353
  • 4
  • 18
  • 1
    Does this answer your question? [Nested 'AND OR' Query in Eloquent](https://stackoverflow.com/questions/25129117/nested-and-or-query-in-eloquent) – El_Vanja Mar 21 '20 at 14:06
  • Yes, it does. Thanks a lot for your help @El_Vanja, sorry for my ignorance. – Dani Fadli Mar 21 '20 at 14:35

2 Answers2

1

You can use where query in the function as below.

$data = DB::table('users')
                ->join('outlets','users.outlet_id','outlets.outlet_id')
                ->where('users.role','Admin')
                ->where(function($q) use ($query) {
                    $q->where('users.nama','like','%'.$query.'%');
                    $q->orWhere('users.username','like','%'.$query.'%');
                    $q->orWhere('outlets.nama_outlet','like','%'.$query.'%');
                    $q->orWhere('users.address','like','%'.$query.'%');
                })->orderBy('users.user_id')
                ->get();
Dilip Hirapara
  • 14,810
  • 3
  • 27
  • 49
1

Try this one:

            $data = DB::table('users')
                ->join('outlets','users.outlet_id','outlets.outlet_id')
                ->where('users.role','Admin')
                ->Where(function($query) use ($name){
                  $query->where('users.nama','like','%'.$query.'%')
                    ->orWhere('users.username','like','%'.$query.'%')
                    ->orWhere('outlets.nama_outlet','like','%'.$query.'%')
                    ->orWhere('users.address','like','%'.$query.'%');
                })                       
                ->orderBy('users.user_id')
                ->get();
Khetesh kumawat
  • 681
  • 7
  • 15
  • Your answer is similar to Dilip Hirapara's answer and it fixes my problem. But I have to make Dilip Hirapara's answer as the best answer because he answered first. But anyway, thanks a lot. – Dani Fadli Mar 21 '20 at 14:37