53

How to use break or continue with Laravel Eloquent Collection's each method. My code is this:

$objectives->each(function($objective) {
        Collection::make($objective)->each(function($action) {
            Collection::make($action)->each(function($success_indicator) {
                Collection::make($success_indicator)->each(function($success_indicator) {
                    echo 'hi';
                    continue;
                });
            });
        });
    });
Nasif Md. Tanjim
  • 3,862
  • 4
  • 28
  • 38
  • As workaround for `break` you can use the `first` method on collection. It will stop iterating when you return the first `true` result. But it's better to show us what you are really trying to do, why you need this `break` and `continue`. – Anatoliy Arkhipov Apr 19 '15 at 05:33
  • for some case we can use `exit();` in place of **continue** , **return true;** **return true;** solution is given below. – pankaj Jan 11 '22 at 12:38

3 Answers3

94

We can return true/false true for continue, false for break

Continue:

collect([1,2,3,4])->each(function ($item){
            if ($item === 2) {
                return true;
            }
            echo $item;
});

Output: 1 3 4

Break:

collect([1,2,3,4])->each(function ($item){
            if ($item === 2) {
                return false;
            }
            echo $item;
});

Output: 1

vijaykumar
  • 4,658
  • 6
  • 37
  • 54
  • 12
    actually you can just `return;` for `continue` as in order to do `break` you need to explicit return `false`. – Emanuel A. Mar 18 '20 at 20:48
  • not working .. return collect($models->items())->each(function ($item, $index){ if( $item->quoteReturnDetail){ return false; } echo "test"; }); – pankaj Jan 11 '22 at 12:32
52

To continue, just return out of the inner function. To break, well..

If you're using Laravel 5.1+, you can return false to break the loop:

$objectives->each(function($objective) {
    collect($objective)->each(function($action) {
        collect($action)->each(function($success_indicator) {
            collect($success_indicator)->each(function($success_indicator) {
                if ($condition) return false;
            });
        });
    });
});

For older version of Laravel, use a regular foreach loop:

$objectives->each(function($objective) {
    foreach ($objective as $action) {
        foreach ($action as $success_indicators) {
            foreach ($success_indicators as $success_indicator) {
                echo 'hi';
                break;
            }
        }
    }
});
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
  • 2
    Documentation of e.g. 5.6 is only stating how to `break` out: https://laravel.com/docs/5.6/collections#method-each not how to skip current item. – Roland Jul 25 '18 at 10:12
  • 4
    @Roland to skip, just `return` nothing. Will exit the current invocation of the function, and will continue to the next item in the loop. – Joseph Silber Jul 26 '18 at 02:54
0

In my case it is working find as i want. i check all code that mentioned above. but not work. Note that my version is Laravel -5.5

 return collect($models->items())->each(function ($item, $index){
        if( !$item->quoteReturnDetail){  echo  $item ;  exit(); }   });
pankaj
  • 1
  • 17
  • 36