For something that is so common across web applications, pagination still can be wildly complicated to implement. Thankfully, Laravel has a built-in concept of pagination, and it’s also hooked into Eloquent results and the router by default.

Paginating database results

The most common place you’ll see pagination is when you are displaying the results of a database query and there are too many results for a single page. Eloquent and the query builder both read the page query parameter from the current query request and use it to provide a paginate() method on any result sets; the parameter is how many results you want per page. Take a look below to see how this works.

Paginating a Query builder response
// PostsController
    public function index()
        return view('posts.index', ['posts' => DB::table('posts')->paginate(20)]);

code above defines that this route should return 20 posts per page, and will define which page the current user is on based on their URL’s page query parameter, if it has one. Eloquent models all have the same paginate() method.