Laravel collection provider array convenient wrapper for working with array collection, in this post we will learn how to use the collapse method in laravel collection.
The collapse() method we can use when multidimensional array need to make into a single array that time developer need to collapse method will help you to make multi-dimensional array convert into single array, simple we can say that multiple array convert into a single array and that will return collapse value tin single array.
collapse() | Laravel Collection Example
Here below working example that can help you with how to use the collapse method used in laravel collection.
// EXAMPLE 1 $data = [ [1,2,3,4,5], [6,7,8,9,10], ]; $collection = collect($data)->collapse(); // OUTPUT EXAMPLE 1 Illuminate\Support\Collection {#6768 ▼ #items: array:10 [▼ 0 => 1 1 => 2 2 => 3 3 => 4 4 => 5 5 => 6 6 => 7 7 => 8 8 => 9 9 => 10 ] } // -------------------------------------------------------- // EXAMPLE 2 $data = [ [1,2,3,4,5], [6,7,8,9,10], ]; $collection = collect($data)->collapse()->first(); // OUTPUT EXAMPLE 2 // 1 // -------------------------------------------------------- // EXAMPLE 3 $data = [ [1,2,3,4,5], [6,7,8,9,10], ]; $collection = collect($data)->collapse()->toArray(); // OUTPUT EXAMPLE 3 array:10 [▼ 0 => 1 1 => 2 2 => 3 3 => 4 4 => 5 5 => 6 6 => 7 7 => 8 8 => 9 9 => 10 ] // -------------------------------------------------------- // EXAMPLE 4 $data = [ [0 => ['array-1']], [1 => ['array-2']], ]; $collection = collect($data)->collapse(); // OUTPUT EXAMPLE 4 Illuminate\Support\Collection {#6768 ▼ #items: array:2 [▼ 0 => array:1 [▼ 0 => "array-1" ] 1 => array:1 [▼ 0 => "array-2" ] ] } // -------------------------------------------------------- // EXAMPLE 5 $data = [ [0 => ['array-1']], [1 => ['array-2']], [1,2,3,4,5] ]; $collection = collect($data)->collapse(); // OUTPUT EXAMPLE 5 Illuminate\Support\Collection {#6768 ▼ #items: array:7 [▼ 0 => array:1 [▼ 0 => "array-1" ] 1 => array:1 [▼ 0 => "array-2" ] 2 => 1 3 => 2 4 => 3 5 => 4 6 => 5 ] }
By referring above example you can easy to understand the collapse method 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 :
- Know All About Laravel Mixin Use In Vue Js With Example
- Default index.php Root Path Change And Remove Public From The Url
- How To Filter In Laravel Query Example
- Select2 ajax example
- Laravel Zaakpay Payment Gateway Integration Tutorial Example
- Laravel 9 REST API with Passport Authentication Tutorial
- Pinelabs Payment Gateway Integration Tutorial In Laravel
- File name required when file upload laravel validation rules
- Looping Variable Laravel Blade File - $loop
- Laravel 9 File manager integration tutorial