get pagination data by a specific page number
Asked Answered
A

2

6

I want to get Data by page number, in normal when we use this code

$jobs = Jobs::paginate(10);

laravel get data by GET "page" parameter that defined in URL, if our URL be http://www.example.com/job?page=2 laravel return data of page 2
I want to do this without using the page parameter in URL

I want you to introduce me something like this

$jobs = Jobs::paginate(10,[
'page'=>2
]);
Ancel answered 24/4, 2019 at 12:6 Comment(0)
A
12

Laravel paginator automatically checks for the the value of page in query string and uses it to paginate the results. The result also automatically generates the next and previous links to help you add them directly

The default paginate method takes the following parameters.

public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null);

so you use like this

$jobs = Jobs::paginate(5, ['*'], 'page', $pageNumber);

The default convention is

example.com/job?page=2 
example.com/job?page=3

Also if you use $jobs->links() to generate the navigation button

Aerophagia answered 24/4, 2019 at 12:23 Comment(0)
C
1

Definition for paginate method is

public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
    {
.......
.......
.......
}

So you can easily change it according to your convenient

$jobs = Jobs::paginate(10,['*'],'page',2);

For more details you can check Laravel Api

Clave answered 24/4, 2019 at 12:23 Comment(1)
@MahdiClash : Please accept Sethu's Answer, it is somewhat similar but He answered 20 Second before my answer. His answer deserve more than my answer.Clave

© 2022 - 2024 — McMap. All rights reserved.