Reputation: 857
I have a query where can i get all the data from related tables and get total 10 rows
.Now i want to set pagination parameter in my query. if my pagenumber=1
and limit=2
it returns 1 and 2 rows
data with page=1
and limit=2
, If I am sending page-3
and limit-2
it returns 5 and 6 rows
and page-3
.if page=null
and limit=null
return all data.
How can I do this.
My Function :
Post::with(['product.categories.attributes'])->whereStatus("Active")->get();
Also how can i pass this in POSTMAN
Upvotes: 0
Views: 3127
Reputation: 3222
All you need is skip / take
https://laravel.com/docs/5.6/queries#ordering-grouping-limit-and-offset
Your code would be something like:
public function show(Request $request) {
$perPage = $request->perpage;
if ($request->page == "") {
$skip = 0;
else {
$skip = $perPage * $request->page;
}
$result = Post::with(['product.categories.attributes'])
->skip($skip)
->take($perPage)
->where("Status", "Active")
->get();
}
Case when $_POST['perpage'] is not set can be explored here:
Upvotes: 3