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 One to Many Eloquent Relationship Tutorial With Example
- Laravel 6 REST API with Passport Tutorial
- How to create Excel file in Laravel 6
- [Fixed] Binary Does Not Exist Laravel
- Laravel 6 - QR Code Generator Example
- How to Generate PDF File Using DomPDF In Laravel 9 Example
- Laravel debugbar install
- Laravel 6 - Preview and Crop Image Before Upload using Ajax
- Laravel broadcasting with redis and socket.io
- Laravel 10 Forum Integration Tutorial Example
