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 Configuration Laravel Supervisor
- Laravel Most Useful Command's Lists With Parameters Example
- How To Get Last Record From The Laravel Collection Using last() Collection Method Possible all Example
- Non-static method Illuminate\Http\Request::method() should not be called statically
- Arr::collapse() | Laravel Helper Function
- Laravel 9 Google Drive File Upload/Store Tutorial.
- Laravel debugbar install
- How To File Upload AWS s3 Bucket In Laravel 9
- How to get images from outside project directory laravel
- Array Convert Into Json Object