如何使用break or continue with Laravel Eloquent Collection的each方法?

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

如何使用 break 或 continue Laravel Eloquent Collection 的 each 方法。 我的代码是这样的:

$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;
                });
            });
        });
    });

continue,刚好return出内层函数。给break,嗯..

如果您正在使用 Laravel 5.1+,您可以 return false 来打破循环:

$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;
            });
        });
    });
});

对于 Laravel 的旧版本,使用常规 foreach 循环:

$objectives->each(function($objective) {
    foreach ($objective as $action) {
        foreach ($action as $success_indicators) {
            foreach ($success_indicators as $success_indicator) {
                echo 'hi';
                break;
            }
        }
    }
});

我们可以 return true/false true continue, false break

继续:

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

输出: 1 3 4

中断:

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

输出: 1

在我的例子中,它正在按我想要的方式工作。我检查了上面提到的所有代码。但不起作用。注意我的版本是Laravel-5.5

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