PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, July 31, 2022

[FIXED] What is the difference between chunk and pagination in Laravel?

 July 31, 2022     chunking, laravel, pagination, php     No comments   

Issue

I already read some article but I am still confused. I n pagination it will execute a query when load a page but what happen in chunk ? I read https://laravel-school.com/posts/laravel-pagination-vs-chunk-51 this article and so on.


Solution

Now pagination works on a per page basis. So if you for example have 100 records and you retrieve 10 records per page you need to perform 10 queries to get all results. However since it's pagination you have a page number you use to determine which records you will return. For example page 3 will return the records 21-30 and so on. Here you will always only load 10 records in memory and return them. Query wise this works with a limit and offset to determine the correct records.

Now looking at chunk it's actually a bit different. So if we still take the 100 records and we chunk them by 10 records we still parse all the 100 records. However it will only load 10 records in memory at a time. So it's using a similar query approach using limit and offset, however it will eventually return all results, but in batches/chunks instead of only return 1 set of records.

The documentation also does a good job of explaining this: https://laravel.com/docs/9.x/eloquent#chunking-results



Answered By - dz0nika
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing