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?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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
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);
Please or to participate in this conversation.