Support the ongoing development of Laravel.io →
posted 1 week ago
Laravel
Last updated by @tvbeek 1 week ago.
0
moderator

Hello @bumbar

I think is the solution you want have.

$orders = collect([
    ['id' => 33, 'OrderNumber' => 505, 'user_id' => 55],
    ['id' => 22, 'OrderNumber' => 505, 'user_id' => 152],
    ['id' => 20, 'OrderNumber' => 505, 'user_id' => 130],

    ['id' => 96, 'OrderNumber' => 494, 'user_id' => 209],
    ['id' => 95, 'OrderNumber' => 494, 'user_id' => 67],

    ['id' => 91, 'OrderNumber' => 492, 'user_id' => 234],
    ['id' => 86, 'OrderNumber' => 492, 'user_id' => 195],
]);

$results = $orders->groupBy(['OrderNumber'])
    ->map(static function(Collection $group): Collection {
        return $group->skip(1);
    })->flatten(1)
;
$results->dump();

Ps. I have updated your post to get the code blocks more readable.

3

Sign in to participate in this thread!

LoadForge

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2022 Laravel.io - All rights reserved.