Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use break or continue with Laravel Eloquent Collection's each method?

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;                 });             });         });     }); 
like image 577
Nasif Md. Tanjim Avatar asked Apr 19 '15 04:04

Nasif Md. Tanjim


1 Answers

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

like image 176
vijaykumar Avatar answered Oct 08 '22 06:10

vijaykumar