Laravel continue looping in transform method

  collections, continue, laravel, loops, php

What is the equivalent of continue while looping in transform()?

I want to do something like this:

$collection->transform(function($item) {
    if ($condition) {
        continue;
    }
    return $item['key'];
});

One option will be to do a ->reject() after transform is completed but is there a way to do it while still looping?

P.S: The goal is to actually return a new collection with one element less. E.g.

[
    '1',
    '2',
]

=> when $condition:

[
    '1',
]

$collection->transform(function($item) {
    if ($condition) {
        return ['']; // <- This didn't work!
    }
    return $item['key'];
});

Source: Ask PHP

LEAVE A COMMENT