minaremonshaker's avatar

how can i make filter , search , sort functionality in blade , i am not using api

I want to implement search functionality along with sorting (orderBy) and pagination (per page). However, I'm having trouble customizing the query string in my Blade form to support all of these features. For example, I am using the Spatie Laravel Query Builder package, which expects URLs in this format: GET /users?filter[name]=john&filter[email]=gmail How can I customize my form’s GET request query string to match this format properly? // GET /users?filter[name]=john&filter[email]=gmail

        <form class="" method="GET" action="{{ route('dashboard.users.filter') }}">
            <div class="flex justify-between">
                <input type="text" id="large-input" class="block  p-4 text-gray-900 border border-gray-300 rounded-lg bg-gray-50 text-base focus:ring-blue-500 focus:border-blue-500" name="filter[search]" placeholder="search..." >
                <select class="g-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block p-2.5 " name="sort">
                    <option value="-1">Sort By</option>
                    <option value="id">id</option>
                    <option value="first_name">first name</option>
                </select>
            </div>
            <div class="my-3">
                <button type="submit" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 me-2 mb-2 dark:bg-blue-600 dark:hover:bg-blue-700 focus:outline-none dark:focus:ring-blue-800">Apply</button>
            </div>
        </form>

0 likes
7 replies
martinbean's avatar
Level 80

@minaremonshaker You would just set the name attribute on an input to be what you’re expecting:

<label for="filter_name">Filter by name</label>
<input id="filter_name" name="filter[name]" type="text">

<label for="filter_email">Filter by email</label>
<input id="filter_email" name="filter[email]" type="email">
jlrdw's avatar

Also you can use query scopes to filter.

martinbean's avatar

@minaremonshaker Don’t over-think things. If a parameter doesn’t have a value that it will just be disregarded. If you submit a form, then all fields’ values will be submitted. That’s just how forms work.

Please or to participate in this conversation.