chunk() laravel collection will divide the array into given param. In this method param must need to which you want to divide. chunk() method in the collection you can easy to use to dividing the collection into different parts this will convert this will divide into many arrays by given single array.
Sometimes developers need to divide the array into different many arrays the time array chunk method we can use.
In model eloquent sometimes need to apply bulk action that time array chunk will take bulk action in the collection, for example, say bulk update in the collection.
chunk() | Laravel Collection Example
Here below a working live example that will help you to understand how to use of chunk method in the collection or bulk action in the eloquent model.
// EXAMPLE 1 $data = [ 1,2,3,4,5,6,7,8,9,10 ]; $collection = collect($data)->chunk(4); // OUTPUT EXAMPLE 1 Illuminate\Support\Collection {#6778 ▼ #items: array:3 [▼ 0 => Illuminate\Support\Collection {#6768 ▼ #items: array:4 [▼ 0 => 1 1 => 2 2 => 3 3 => 4 ] } 1 => Illuminate\Support\Collection {#6775 ▼ #items: array:4 [▼ 4 => 5 5 => 6 6 => 7 7 => 8 ] } 2 => Illuminate\Support\Collection {#6777 ▼ #items: array:2 [▼ 8 => 9 9 => 10 ] } ] } //------------------------------------------------------- // EXAMPLE 2 $data = [ 1,2,3,4,5,6,7,8,9,10 ]; $collection = collect($data)->chunk(5); // OUTPUT EXAMPLE 2 Illuminate\Support\Collection {#6777 ▼ #items: array:2 [▼ 0 => Illuminate\Support\Collection {#6768 ▼ #items: array:5 [▼ 0 => 1 1 => 2 2 => 3 3 => 4 4 => 5 ] } 1 => Illuminate\Support\Collection {#6775 ▼ #items: array:5 [▼ 5 => 6 6 => 7 7 => 8 8 => 9 9 => 10 ] } ] } //------------------------------------------------------- // EXAMPLE 3 $data = [ 1,2,3,4,5,6,7,8,9,10 ]; $collection = collect($data)->chunk(5)->first(); Illuminate\Support\Collection {#6768 ▼ #items: array:5 [▼ 0 => 1 1 => 2 2 => 3 3 => 4 4 => 5 ] } //------------------------------------------------------- // EXAMPLE 4 $data = [ 1,2,3,4,5,6,7,8,9,10 ]; $collection = collect($data)->chunk(5)->toArray(); array:2 [▼ 0 => array:5 [▼ 0 => 1 1 => 2 2 => 3 3 => 4 4 => 5 ] 1 => array:5 [▼ 5 => 6 6 => 7 7 => 8 8 => 9 9 => 10 ] ]
By refering above example you can easy to understand about chunk use in laravel collection.
Thanks...
We always thanks to you for reading our blogs.
Dharmesh Chauhan
(Swapinfoway Founder)Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us
Haresh Chauhan
(Co-Founder)We Are Also Recommending You :
- Cashfree Payment Gateway Integration In Laravel
- How To Get Current Request Method Post Patch Get In Laravel - PHP
- Laravel 6 call function undefined str_slug() - fix
- Import Export Database in JSON Format Laravel
- Laravel 9 Google Drive File Upload/Store Tutorial.
- UPI Payment Gateway Integration in PHP/Laravel
- Single Session User Login in Laravel
- Get Current Route Name Laravel
- Laravel 10 Firebase Web Push Notification Tutorial
- How to Generate PDF File Using DomPDF In Laravel 9 Example