Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

NoLAstNamE's avatar

problem with appending query string

So I'm having a problem with appending the query string to the results, I have simple filtering (select/dropdown) in my form and a search box (input).

The problem is the result of the steps below:

  1. IF I do select an item in the dropdown and then;

  2. Type to search input, and press the SUBMIT button, the query string from the dropdown is removed and only the query string from the search is appended to the URL.

Example URL after doing the step above:

http://example.test/users?search=bar

The final URL should be:

http://example.test/users?filter=foo&search=bar

The HTML form

<a href="{{ route('users', [
        'filter' => 1, 
        'search' => request()->get('search')
    ]) }}">

    Filter #1 
</a>
<a href="{{ route('users', [
        'filter' => 2, 
        'search' => request()->get('search')
    ]) }}">

    Filter #2 
</a>

<form action="{{ route('users', [
    'filter' => request()->get('filter'), 
    'search' => request()->get('search')
]) }}" method="GET">
    <input type="text" name="search" value="{{ request()->get('search') }}">

    <button type="submit"></button>
</form>

Controller

public function users(Request $request) {
    $users = User::query()
        ->where('filter', $request->get('filter'))
        ->where('name', 'like', '%'.$request->get('search').'%')
        ->paginate(10);

    $users->appends([
        'search' => $request->get('search'),
        'filter' => $request->get('filter')
    ]);
}

I also tried appending ->withQueryString() after paginate(10) but it's still not doing the trick.

I don't know why the filter query string is not appended after submitting the search form.

0 likes
5 replies
tykus's avatar
<form action="{{ route('admin.users') }}" method="GET">
    <input type="text" name="filter" value="{{ request()->get('filter') }}">
    <input type="search" name="search" value="{{ request()->get('search') }}">
    <button type="submit"></button>
</form>

The filter input can be hidden if necessary.

1 like
NoLAstNamE's avatar

@tykus Now it's appending but another problem has shown, it's always in the address bar/URL with an empty value which is not ideal because in my controller I access the value of it in the where condition.

I mean, by default the filter is empty and not present in the URL query string.

tykus's avatar
tykus
Best Answer
Level 104

@benjamin1509 in that case; you can either (i) only include the form input if there is a filter in the query params:

<form action="{{ route('admin.users') }}" method="GET">
    @if (request()->has('filter')
        <input type="text" name="filter" value="{{ request()->get('filter') }}">
    @endif
    <input type="search" name="search" value="{{ request()->get('search') }}">
    <button type="submit"></button>
</form>

or, (ii) handle null or empty filter values in the Controller:

$users = User::query()
    ->when(
        $request->get('filter'),
        fn ($builder, $filter) => $builder->where('filter', $filter)
    )
    ->where('name', 'like', '%'.$request->get('search').'%')
    ->paginate(10);
1 like

Please or to participate in this conversation.