Laravel each() Collection Method Example

The each method laravel collection have callback function It iterate over the items in the collection and passes each item to a closure. If you would like to stop iterating through the items, you may return false from your closure. it’s iteration and return the reference to the original collection.

Example 1

if we want to show all collection

$collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
        $collection->each(function ($value) {
            echo $value;
}); 

output:

12345678910

Example 2

if we want to stop iterating then you need simply pass if statements and return false


$collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
        $collection->each(function ($value) {
            if ($value > 5) {
                return false;
            }
            echo $value;
        });

output:

12345

Example 3

in key value array we can also use each method


$collection = collect([
            [
                'name'      =>  'Samsung S10',
                'brand'     =>  'samsung',
                'battery'   =>  '5000 mAh',
                'is_5g'     =>    1,
                'ram'       =>  '10GB',
                'camera'    =>  '64Mb',
                'price'     =>  95000
            ],
            [
                'name'      =>  'Redme Note 8',
                'brand'     =>  'xiaomi',
                'battery'   =>  '4000 mAh',
                'is_5g'     =>    0,
                'ram'       =>  '6GB',
                'camera'    =>  '24Mb',
                'price'     =>  15000
            ],
            [
                'name'      =>  'Samsung S10 mini',
                'brand'     =>  'samsung',
                'battery'   =>  '4000 mAh',
                'is_5g'     =>    1,
                'ram'       =>  '8GB',
                'camera'    =>  '48Mb',
                'price'     =>  70000
            ],
            [
                'name'      =>  'Apple iPhone 12 Pro Max',
                'brand'     =>  'apple',
                'battery'   =>  '3687 mAh',
                'is_5g'     =>    1,
                'ram'       =>  '6GB',
                'camera'    =>  '12Mb',
                'price'     =>  120000
            ],
            [
                'name'      =>  'Apple iPhone X',
                'brand'     =>  'apple',
                'battery'   =>  '2716 mAh',
                'is_5g'     =>    0,
                'ram'       =>  '3GB',
                'camera'    =>  '12Mb',
                'price'     =>  80000
            ],
        ]);

lets prints all collection items name


$collection->each(function ($item, $key) {
            echo '</br>' . $item['name'];
});

output:

Samsung S10

Redme Note 8

Samsung S10 mini

Apple iPhone 12 Pro Max

Apple iPhone X


Example 4

if you want stop or break then use if statements and compare values and return false


 $collection->each(function ($item, $key) {
            if ($item['price'] > 100000) {
                return false;
            }
            echo '</br>' . $item['name'];
        });

output:

Samsung S10

Redme Note 8

Samsung S10 mini

Tags: