![](/img/trans.png)
[英]Laravel 4 's all() method started to return Eloquent collection lately - why?
[英]How to use break or continue with Laravel Eloquent Collection's each method?
如何使用 Laravel Eloquent Collection 的 each 方法使用 break 或 continue。 我的代碼是這樣的:
$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;
});
});
});
});
我們可以返回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
要continue
,剛剛return
的內部函數出來。 break
,好吧..
如果你使用的是Laravel 5.1+,你可以返回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;
}
}
}
});
就我而言,它正在按我的意願工作。 我檢查了上面提到的所有代碼。 但不工作。 注意我的版本是Laravel -5.5
return collect($models->items())->each(function ($item, $index){
if( !$item->quoteReturnDetail){ echo $item ; exit(); } });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.