Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jeroenvanrensen's avatar

Laravel collections: Do something on the collection

Hi everyone,

I have a Laravel collection and I want to perform some logic on that collection. I found a way to do it:

$collection
    ->when(true, function($collection) {
        return $collection instanceof Collection ? $collection : $collection[0];
    });

But isn't there a cleaner way? Something like do or execute?

Thank you! Jeroen

0 likes
20 replies
Snapey's avatar

depends what that something is

there are loads of collection methods to choose from and of course they can also be chained

your code posted here gives no clue what you are trying to achieve?

1 like
jeroenvanrensen's avatar

Hi @snapey,

If the $collection is an Eloquent Collection, I want to keep it and else I want to take the first item to chain new items on.

I hope you understand.

Thanks!

Snapey's avatar

a simpler way is just with an if statement.

if(! $collection instanceof Collection { 
    $collection = $collection[0];
}

then its easier to understand what is happening.

Personally, I would focus on making sure you always get a collection in the first place

untymage's avatar

No need to check if its collection instance or not:

$collection
    ->when(true, fn(Collection $c) => $c->first());

Because laravel pass the collection instance behind the scene

jlrdw's avatar

What do you mean by Chain it on, could you give an example. I would stick with just working with SQL queries.

1 like
jeroenvanrensen's avatar

@jlrdw yes:

$collection
    ->when(true, function($collection) {
        return $collection instanceof Collection ? $collection : $collection[0];
    })
    ->shuffle()
    ->take(5);
jlrdw's avatar
$shuffled = $collection->shuffle()->take(3);

Try that.

dd($shuffled);
Snapey's avatar

is this such a hardship?

if(! $collection instanceof Collection { 
    $collection = $collection[0];
}

$collection->shuffle()
    ->take(5);
jlrdw's avatar

@snapey just curious is my answer different from yours?

Above in a previous answer this part:

if(! $collection instanceof Collection { 
    $collection = $collection[0];
}

Was previously accomplished. I was just showing the part that would come after that.

I did however a assign a variable to work with.

Snapey's avatar

@jlrdw no, you are correct, the output from shuffle must be stored in a new variable.

jeroenvanrensen's avatar

@snapey @jlrdw,

Thanks for these answers, but they don't really answer my question.

I really would like to chain it on, but I guess there is not any method available.

Thank you! Jeroen

Snapey's avatar
Snapey
Best Answer
Level 122

I was trying to say that, despite my long experience here, it was not immediately obvious what you were doing with the when collection method - especially with the boolean being true

perhaps then

$collection = 
	($collection instanceof Collection ? $collection : $collection[0])
	->shuffle()
    ->take(5);
1 like
neilstee's avatar

@jeroenvanrensen I think what is weird with your approach is you are expecting your $collection variable to be a Collection in the first place by using when.

// what if $collection is an array?
// "when" will be undefined in this case
$collection
    ->when(true, function($collection) {
        return $collection instanceof Collection ? $collection : $collection[0];
    });

@snapey solution is clean and safe IMO

if(! $collection instanceof Collection) { 
    $collection = $collection[0];
}

$collection->shuffle()->take(5); // but assumes [0] is a collection

So if you would want a 1 liner, chain method and preventing "undefined" errors, you might want to try this:

$collection = ($collection instanceof Collection ? $collection : collect($collection[0]))->shuffle()->take(5);

I know your goal is to chain those methods but maybe that is not the best approach and readable in the first place? For me @snapey 's approach using if is cleaner.

neilstee's avatar

on that note, $collection[0] can be undefined so you might want to collect($collection[0] ?? []) which adds up to unreadability.

jeroenvanrensen's avatar

No, I was making the difference between a normal collection (Illuminate\Support\Collection) and an Eloquent collection (`Illuminate\Database\Eloquent\Collection')

Snapey's avatar

@neilstee makes a good point. The ->when function cannot be called if it is not a collection

kodyxgen's avatar

Hi, i maybe something like this ?

$collection = (is_array($collection) ? collect($collection)->first() : $collection)
    ->shuffle()
    ->take(5);

Please or to participate in this conversation.