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

ianspangler's avatar

Paginator: Get the current page from ID in URL

I am trying to add simple next/previous navigation to a large set of profile/ post pages. On each post, I would like to show links to view the next and previous post, but each page is just a single post. Or, in other words, there is only ONE post per page. My plan so far is to use the Paginator class, but the problem is figuring out how to get the correct "page" from the post's ID. Here is just a simplified dummy version of the code, showing what I am aiming to do:

Route::get('posts/{id}', 'PostsController@show');

public function show($id) {
   $perPage = 1;
   $currentPage  = getCurrentPage($id); //IS THERE SOME BUILT-IN METHOD LIKE THIS??

   $posts = Post::where(...some conditions...)->toArray();
   $thisPost = array_slice($posts, $perPage * ($currentPage - 1), $perPage);
    return  new Paginator($thisPost, $perPage, $currentPage);
}
0 likes
0 replies

Please or to participate in this conversation.