Mahdi mehrabi
Mahdi mehrabi

Reputation: 1744

get pagination data by a specific page number

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
]);

Upvotes: 6

Views: 12353

Answers (2)

Niklesh Raut
Niklesh Raut

Reputation: 34914

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

Upvotes: 1

Sethu
Sethu

Reputation: 1379

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

Upvotes: 12

Related Questions