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

Ligonsker's avatar

How to change path and query string when using LengthAwarePaginator?

I created a custom LengthAwarePaginator using this answer from SO: https://stackoverflow.com/a/63392687/19815685

But there were 2 issues: first the path was not correct, but I was able to change that by hardcoding the URL like so:

$path = 'my-path';
$options = ['path' => $path];
return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);

But I couldn't find out how to change the query string. Now it shows:

example.com/my-path?page=3

Is it possible to use the LengthAwarePaginator to get the current path (if not specified, it will use the root URL) without hardcoding it, and also change the default query string from page to something else?

0 likes
19 replies
tykus's avatar

There is a setPath public function available on the AbstractPaginator class; you can use this to optionally set a path. Similarly there is a setPageName method which allows you to change the default page query param key

1 like
Ligonsker's avatar

@tykus thanks, going to try now. I think the docs on Pagination are too concise

Ligonsker's avatar

@tykus thanks, will go to the source more instead of just the docs

Also, the methods setPath and setPageName worked. However, the links don't work anymore: The show up, and also the url changes to what I set, but the page itself doesn't change, it remains on page 1. this is how I used it:

$paginator = new LengthAwarePaginator();
$paginator->setPath('myPath');
$paginator->setPageName('pageName');
Ligonsker's avatar

@tykus yes but I think the problem is somewhere here:

$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
$items = $items instanceof Collection ? $items : Collection::make($items);
return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);

maybe the original way of using $page ?: (Paginator::resolveCurrentPage() ?: 1); doesn't work anymore because I changed the name and now there is an issue with resolveCurrentPage?

Update: I just realized that even though the links are showing my custom query string, it only works if I use the old query string with the value page (If I change in the url to ?page=2 it will go to the correct page)

tykus's avatar
tykus
Best Answer
Level 104

@Ligonsker if you are not using the standard page parma, then you need to pass the custom page name to the resolver, e.g.

Paginator::resolveCurrentPage('pageName')
2 likes
Tray2's avatar

Is there any particular reason that you don't use one of the built in ones that Laravel provides?

Model::where('col_1',  15)->paginate(25);

//Or

Model::where('col_1',  15)->simplePaginate(25);
1 like
Tray2's avatar

@Ligonsker then I suggest moving the query into a database view and then query it like a regular table.

1 like
Ligonsker's avatar

@Tray2 I never used views before, and it needs to be ready soon. But I am going to read about that. With view I basically store the complex query, give it some name and then I can query it with something like regular select? Or it acts as a table name?

Ligonsker's avatar

@Tray2 I did not read all the article yet, but can you help me get the idea:

Let's say I have the Messages table with all the information. And I need some specific data from the Messages table. In my case - the oldest messages from each user. I'll create a view called OldestMessageFromEachUser, and what it does is to run the complex query that I want from the Messages table and "clone" the result from Messages table to the OldestMessageFromEachUser view table, and every time Messages updates, so will OldestMessageFromEachUser, and I can read from it?

Tray2's avatar

@Ligonsker Yeah kinda, the view is a query that exists in the database as a view and you make your queries agains that query.

You could say that it's similar to an inline view, but the code is stored in the database.

SELECT t1.id
FROM (SELECT id FROM table1) AS t1
1 like
Ligonsker's avatar

@Tray2 haha I don't even know what an inline view is, my DB knowledge is not that broad, I gotta read that article :}

Ligonsker's avatar

@Tray2 thank you, got it! I read about it also in your article, here is the example:

SELECT t.*
FROM (SELECT id, title, created_at, 'books' source
      FROM books
      UNION ALL
      SELECT id, title, created_at, 'movies'
      FROM movies
      UNION ALL
     SELECT id, title, created_at, 'records'
     FROM records) t
ORDER BY t.created_at DESC
LIMIT 10;
Tray2's avatar

@Ligonsker Exatly, that is a pretty nice example of both using an inline view and using a union to get data from three different tables at once, so that you can list the latest 10 rows.

1 like

Please or to participate in this conversation.