如何用断续或继续与拉雅维尔雄辩的收集的每一种方法?

时间:2022-10-16 07:42:25

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

2 个解决方案

#1


16  

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:

如果您正在使用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;
            });
        });
    });
});

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

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

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

#2


7  

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

我们可以返回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

输出:1 3 4

Break:

打破:

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

Output: 1

输出:1

#1


16  

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:

如果您正在使用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;
            });
        });
    });
});

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

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

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

#2


7  

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

我们可以返回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

输出:1 3 4

Break:

打破:

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

Output: 1

输出:1