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 :
- How to Create Zip File and Download in Laravel
- Firebase Dynamic Links Shorten URL PHP Function
- Laravel 9 Telescope Integration Tutorial
- Create Custom Route File With New Service Provider Laravel 8 9
- How To Insert Big Data In The Database Laravel?
- How to use Date Format Validation in Laravel?
- How To Get Current Request Method Post Patch Get In Laravel - PHP
- Laravel Tap Payment Gateway Integration Tutorial Example
- PhonePe Payment Gateway Integration in Laravel 12 (Step-by-Step Guide 2026)
- Livewire Create Multiple Select2 Dynamic Dropdown In Laravel 9 8 Example