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 :
- Laravel 10 Sanctum API Authentication Tutorial Example
- [issue] Possible All Way To Change Asset Path And Add Public To Asset Path In Laravel
- Laravel Email Send
- What are accessors and mutators In Laravel
- Laravel 12 REST API Tutorial for Beginners (Step-by-Step Guide 2026)
- Custom Validation Message Laravel
- Livewire File Upload Laravel
- Unique violation: 7 ERROR: duplicate key value violates unique constraint "users_pkey" DETAIL: Key (id)=(1) already exists.
- How to Get Browser Name and Version in Laravel
- Laravel 10 Restful API Passport Authentication Tutorial