@tisuchi Thank you for the reply. But, I am afraid I can't use your suggestion because using search() and then extra line to forget() inside the loop. I am afraid because of them my code goes slow.
But, I figured it out to keep using filter() then shift() like this:
$accounts = Account::with('server')->get();
// Refactor this
$channels = $accounts->pluck('server.channel')->values()->unique()->values()->toArray()
>map(function ($channel) use ($accounts) {
return [
'channel' => $channel,
'accounts' => $accounts->filter(function ($account) use ($channel) {
return $account->server->channel === $channel;
})->values()
];
});
$total = 120;
$container = collect();
while ($total > 0) {
foreach ($channels as $channel) {
if ($total === 0) break;
// Refactor this
if ($channel['accounts']->isNotEmpty()) {
$container->push(
$container['accounts']->shift();
);
}
$total--;
}
}
return $container;
But, I need the answer why using another \Illuminate\Support\Collection methods before shift() keep resetting the Collection? I have try using another code like this:
$numbers = collect()->range(1, 10)->values();
$container = collect();
$index = 0;
while ($index < 6) {
if ($numbers->isNotEmpty()) {
$container->push($numbers->shuffle()->shift());
}
$index++;
}
return [$numbers->count(), $container->count()];
And then the result is [10, 6]. I assume using another \Illuminate\Support\Collection methods before shift() keep resetting the $numbers Collection.