0

I'm working with Laravel 8 and at users.blade.php where all of the users of the website appears, I wanted to add a search form for searching the names, mobile number & other information related to a user:

<form method="GET" action="">
    <div>
        <div class="form-group">
            <div class="row">
                <div class="col-md-3">
                    <label for="name">Name or Last Name</label>
                    <input type="text" class="form-control" name="name" 
                                                value="{{ request()->query('name') }}">
                </div>
                <div class="col-md-3">
                    <label for="mnumber">Mobile Number</label>
                    <input type="text" class="form-control" name="order"
                                               value="{{ request()->query('mnumber') }}">
                </div>
                <div class="col-md-3">
                    <label for="ucode">User Code</label>
                    <input type="text" class="form-control" name="product"
                                                value="{{ request()->query('ucode') }}">
                </div>
                <div class="col-md-3">
                    <label for="ncode">National Number</label>
                    <input type="text" class="form-control" name="order"
                                                value="{{ request()->query('ncode') }}">
                </div>
            </div>
        </div>
   </div>
</form>

Then at the Controller I tried this:

 public function index()
    {
        $users = User::query();

        if($keyword = request('name')) {
            $users->where('name' , 'LIKE' , "%{$keyword}%");
        }

        $users = $users->latest()->paginate(20);

        return view('admin.users.all', compact('users'));
    }

But now the problem is when I fill the name field with a user name that already exists in the DB, it does not show that custom user because the if($keyword = request('name')) { condition does not run & request('name') is null!

In other words, when I submit the data using this url:

http://localhost:8000/admin/users?name=ENTERED_NAME&mnumber=&ucode=&ncode=

The result does not appear but when I submit it like this:

http://localhost:8000/admin/users?name=ENTERED_NAME

it shows result correctly!

So how can I properly search for the name field properly while the other fields are in the form?

japose7523
  • 29
  • 2
  • 15
  • I can't reproduce ... But the URL you show is not generated by the form you've shown, 3 of 4 the field names don't match. Can you edit and post your *real* code? Also 1) How are you submitting the form? There is no button in the code you've shown. 2) 2 of your inputs have the same name `name="order"`, and so the first will be overwritten and lost. – Don't Panic Jul 27 '22 at 08:46
  • @Don'tPanic It won't happen and you can have two inputs with the same name in a page. – WebPajooh Jul 27 '22 at 19:25
  • @WebPajooh what won't happen? "*you can have two inputs with the same name*" - you can, yes, but there is no point bcs you will only get 1 of the values in PHP. [You would need to use a name like `name="order[]"`](https://stackoverflow.com/questions/7880619/multiple-inputs-with-same-name-through-post-in-php) to get all values. – Don't Panic Jul 28 '22 at 07:48
  • have you considered using when(). for when request exist? On using when you can do specific. – kuro Jul 28 '22 at 12:51

3 Answers3

3
     public function index(Request $request)
            {
                $users = User::query();
         

                if(isset($request->input('name'))) {
                    $keyword = $request->input('name');
                    $users->where('name' , 'LIKE' , "%{$keyword}%");
                }
        
                $users = $users->latest()->paginate(20);
        
                return view('admin.users.all', compact('users'));
            }
Shozab javeed
  • 232
  • 2
  • 11
0

HTML Form Code

  <form method="GET" action="">
        <div>
            <div class="form-group">
                <div class="row">
                    <div class="col-md-3">
                        <label for="name">Name or Last Name</label>
                        <input type="text" class="form-control" name="name" value="{{ request()->query('name') }}">
                    </div>
                    <div class="col-md-3">
                        <label for="mnumber">Mobile Number</label>
                        <input type="text" class="form-control" name="mnumber" value="{{ request()->query('mnumber') }}">
                    </div>
                    <div class="col-md-3">
                        <label for="ucode">User Code</label>
                        <input type="text" class="form-control" name="ucode" value="{{ request()->query('ucode') }}">
                    </div>
                    <div class="col-md-3">
                        <label for="ncode">National Number</label>
                        <input type="text" class="form-control" name="ncode" value="{{ request()->query('ncode') }}">
                    </div>
                </div>
            </div>
       </div>
    </form>

Controller Code Here in your code I suspect issue that in if condition you are not checking double equal you just place single equal. so I resolved it and place.

public function index()
    {
        $users = User::query();

        if($keyword == request('name')) {
          $users->where('name' , 'LIKE' , "%{$keyword}%");
        }

        $users = $users->latest()->paginate(20);

        return view('admin.users.all', compact('users'));
    }

Modified Controller Code Here I write code with some Laravel standard and full code for other keywords also.

 public function index()
    {
        $users = User::query();

        if($request->has('name')) {
          $keyword = $request->input('name');
          $users->where('name' , 'LIKE' , "%{$keyword}%");
        }

        if($request->has('mnumber')) {
          $keyword = $request->input('mnumber');
        }

        if($request->has('ucode')) {
          $keyword = $request->input('ucode');
        }
        
        if($request->has('ncode')) {
          $keyword = $request->input('ncode');
        }

        $users = $users->latest()->paginate(20);

        return view('admin.users.all', compact('users'));
    }
Iamshriyogi
  • 360
  • 1
  • 13
0

I use when() instead of if.. else.. you could try the query below and check if it works. I use similar types of query to search user.

$user = User::query()
            ->when(request()->has('name'), function ($query) {
                $query->where('name', 'like', '%' . request('name') . '%');
            })
            ->when(request()->has('mnumber'), function ($query) {
                $query->where('mnumber', 'like', '%' . request('mnumber') . '%');
            })
            ->when(request()->has('ucode'), function ($query) {
                $query->where('ucode', 'like', '%' . request('ucode') . '%');
            })
            ->when(request()->has('ncode'), function ($query) {
                $query->where('ncode', 'like', '%' . request('ncode') . '%');
            })
            ->paginate(20);

I think this might work without any modification to the view.

kuro
  • 194
  • 9