Pagination in laravel..
Raidah Fairuz Nashra
Posted on January 29, 2024
Here we have lots of data to show then it's tough to show all those in one page so dividing the records in many pages is called pagination. Pagination is the setting of the query's "limit" and "offset" based on current pages. Laravel delete the value automatically and the value inserted into links generated by the paginator. The number of arguments passed to the method is the number of items we want to be displayed in per page.
1.In controller page $products=Product::orderBy('id','desc')->paginate(10); instead of "->get()" we have to write "->paginate()". 10 products will display in one page.
2."Provider" "AppServiceProvider" we have to use "pagination\paginator" after declaration
public function boot()
{
Paginator::useBootstrap();
}
3.In view page where we show the output
{!! $products->links() !!}
simple pagination
Before retrieving the records from DB The method counts the total number of records which are matched by the query.If we want to display only "Next" and "Previous" links in application then we use "simplePaginate".
Paginating Eloquent Result
We paginate "Eloquent" queries. To paginate the query builder results.
it is possible to set cause and condition after over coming other constraints/limitations.
$users= User::where ('votes', '>', 100)->paginate(15);
We also can use the simplePaginate here.
*How pagination works: *
limit=20 (requesting only for 20 items per page)
offset=0 (It is starting from 0 item)
let,
request response Server-Side
limit=20&offset=0 page 1 max limit=20
limit=20&offset=0+20 page 2 "
limit=20&offset=40 page 3 "
limit=20&offset=60 page 4 "
limit=20&offset=80 page 5 "
pagination is always compulsory from API request, filtering is optional.
Posted on January 29, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 30, 2024