Laravel Pagination Vs Chunk

Laravel Pagination Vs Chunk

Posted on:August 27, 2019 at 10:00 AM

Have you ever put together Laravel Pagination and Chunk, then realize how it works? Are they same or there is a difference between them? If same, then what is the purpose of having two different options for achieving the same work, if not, then what are the fundamental difference between them. In this post, I will talk about it.

Apparently, it looks the same, but technically it’s not same. It works in a bit different way.

Table of Contents

Open Table of Contents

Pagination

According to the laravel documentation-

The paginate method automatically takes care of setting the proper limit and offset based on the current page being viewed by the user. By default, the current page is detected by the value of the page query string argument on the HTTP request. This value is automatically detected by Laravel, and is also automatically inserted into links generated by the paginator.

Well, let me explain to you a bit about it with a real-life example.

Imagine that, you have a 1000 users record in the users table. Now you want to fetch all the records and then display in view page, so that visitor can able to see all the users. Now, think about how it will be looked like if a page holds 1000 records? Is that readable? Honestly, it’s not user-friendly, to me. So, that’s why the concept come up, pagination where you can show the number of users in your page by the slice. For example, in view page, you want to show 100 users at a time, so that it won’t be too much record for the visitor to read. Once the visitor reaches to the bottom of the list, there should be some number like 1, 2, 3, …, 10 where each number represent the page number.

Laravel Pagination

Now, let’s write a query to get all the users where each page shows 100 users only at a time.

User::latest()
  ->paginate(100);

Technically this query will operate 10 queries (100 records * 10 queries = 1000 records) to get all the records. It will return page numbers where you can click to see a specific range of records. For example, you want to records from 501-600 where page 6 will represent that. Here the query will always load 100 records in the memory to return. The query will works by limit and offset to fetch the correct data.

Chunk

According to the documentation-

If you need to work with thousands of database records, consider using the chunk method. This method retrieves a small chunk of the results at a time and feeds each chunk into a Closure for processing. This method is very useful for writing Artisan commands that process thousands of records. For example, let’s work with the entire users table in chunks of 100 records at a time:

Technically chunk is bit different. Again, if we take 1000 records from the table, and then chunk them by 100 records, still it will parse all the 1000 records. Keep it in your mind that, it will load 100 records in the memory at a time. From the parsed data, it uses a similar query approach limit and offset and set a chunk of data based on user requirement.

For example-


DB::table('users')->orderBy('id')->chunk(100, function ($users) {
    foreach ($users as $user) {
        //
    }
});

Here, it will parse 1000 records (all data from the user table) then chunk them by 100 and load in the memory.

So, that’s are basically different between pagination and chunk. Feel free to share your comments or suggestion to improve this.