ColWayne's avatar

Laravel 8 Pagination using withQueryString() not working as expected

I have this in a model

//Doing a post request and passing a name $searchName = $request->input('name');

//To simplify the issue I am just pulling all users. $user = User::paginate(5)->withQueryString();

return view('dashboard', ['users' => $user]);

In my view i have this for the pagination links. I assume per the docs the withQueryString would add the name I sent in the post. It does not. {{ $user->links() }}

If I use the appends method it adds the name to the pagination links. $user->appends(['name' => 'bobby.hill']);

Any ideas as to why withQueryString() isn't working?

0 likes
8 replies
ColWayne's avatar

That was a typo on my end. I get a return of all users paginating by 5. That all works. What doesnt work is if I do a post request and pass in a name. The name is sent in the request, I can use it in the model but it doesn't get added to the pagination view links by using the withQueryString method

MichalOravec's avatar

Because you sent it through POST request. The query string is sent in the URL of a GET request.

Why do you return view after POST request?

Why do you have model name Users instead of User?

This will work

$users = User::paginate(5)->appends(['name' =>  $request->name]);
ColWayne's avatar

Why do you return view after POST request?

  • I am displaying results from a search

Why do you have model name Users instead of User?

  • Typo on my end when making the post.

The appends method does work but I am using a form and the withQueryString method says it will append all the current requests query string values to the pagination links

MichalOravec's avatar
Level 75

But you use POST request.

Better if you change form to GET request and route as well.

Then withQueryString will work.

Route::get('users', 'YourController')->name('users.index');
<form action="{{ route('users.index') }}" method="GET">
    <input type="text" name="name">

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

Please or to participate in this conversation.