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

feniixx's avatar

Split an Eloquent Collection by half

I would like to know how could i split by half a Collection and if it's not a pair number, then split it like:

Collection count = 15

Collection 1 = 7

Collection 2 = 8

Is that possible?

0 likes
12 replies
thomaskim's avatar

@JSolorzano

I'm not sure if there is a simpler way, but you can do something like this.

$collectionHalves = array_chunk($collection->all(), ceil($collection->count() / 2));

// $collectionHalves[0] = first half of array. If there are 13 total, then this will be 7. If there are 12 total, then this will be 6. This will always be equal or 1 bigger than the other half of the array.
// $collectionHalves[1] = second half of array.
2 likes
phildawson's avatar
Level 26

Theres a method for that.

$half = ceil($collection->count() / 2);
$chunks = $collection->chunk($half);
3 likes
feniixx's avatar

@phildawson here's how i do it


        <?php
            $half = ceil($menu->opciones->count() / 2);
            $chunks = $menu->opciones->chunk(intval($half));
        ?>
        @foreach($chunks as $chunk)
            <div class = "col-md-6" >
                @foreach($chunk as $opcion)
                    @include('Club.menu.opcion',[
                    'opcion' => $opcion
                    ])
                @endforeach
            </div >
        <!-- /col-md-6 -->
        @endforeach

And this is the error i'm getting

array_chunk(): Size parameter expected to be greater than 0
phildawson's avatar

@JSolorzano I would dd($half); out, I'm guessing 0 as no options are being returned for the menu. The intval isn't need btw as ceil will return the next highest integer. I would also avoid php logic in the views if possible.

When you use something you expect I would recommend wrapping to make sure it exists.

@unless($menu->options->isEmpty())

// loop

@endunless
1 like
jekinney's avatar

Seems inefficient, but maybe just me.

I have done this, simplified version:

$collection = Collection::all();
$half = $collection->count() / 2;
$first = $collection->limit($half);
$second = $collection->skip($half);

$complete = compact('first', 'second');

return $complete;

view:

@foreach($complete->first as $first)
    {{ $first->column_name }}
@endforeach

@foreach($complete->second as $second)
    {{ $second->column_name }}
@endforeach

This approach does one query and one time, so in the event something changes while doing the two queries to to get the count and the data you don't have to worry.

Your going over one collection and splitting it up IMO simply and with the least amount of logic required.

1 like
phildawson's avatar

@jekinney That's more convoluted! And a mess if you wanted it split in three or four in future. :p

The chunk doesnt do any queries. It's a clever wrapper for array_chunk making a Collection of Collections. Its very efficient! :)

Please or to participate in this conversation.