phplaravelrequestlaravel-8laravel-request

How to fix search request query problem with multiple form fields


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?


Solution

  • 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.