The beginners guide to Pagination in laravel 5.2

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.

Recommended :  Easy way to manage or add modules in laravel 5.1
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.

About the author

Deven Rathore

I'm Deven Rathore, a multidisciplinary & self-taught designer with 3 years of experience. I'm passionate about technology, music, coffee, traveling and everything visually stimulating. Constantly learning and experiencing new things.

Pin It on Pinterest

Shares