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

tomasosho's avatar

how to paginate query

Original Query

$sort = File::latest()->get()->groupBy(function($item)
        {
            return $item->date;
        });

I tried this, and it doesn't work. it only gives one item

$sort = File::latest()->paginate(50)->groupBy(function($item)
        {
            return $item->date;
        });
0 likes
9 replies
Sinnbeck's avatar

Why are you trying to group the items? What will they be used for?

You can get the items for the page like this

$items = File::latest()->paginate(50)->items();
1 like
tomasosho's avatar

@Sinnbeck I'm grouping items by date, so items appear underneath dates they were created

Sinnbeck's avatar

@tomasosho You can try this then

$sort = File::latest()->paginate(50)->items()->groupBy(function($item)
        {
            return $item->date;
        });
Sinnbeck's avatar

@tomasosho Convert to collection then

$sort = collect(File::latest()->paginate(50)->items())->groupBy(function($item)
        {
            return $item->date;
        });
1 like
Sinnbeck's avatar

@tomasosho Well I suggest you pass this stuff next to your pagination so you have each one

$pagination =File::latest()->paginate(50);
$grouped = collect($pagination->items())->groupBy(function($item)
        {
            return $item->date;
        });

return view('myview', compact('pagination', 'grouped'));
1 like
Tray2's avatar

Then it probably should be

$sort = File::latest()>groupBy(function($item)
        {
            return $item->date;
        })->paginate(50);
tomasosho's avatar

@Tray2 This was the first one i tried. But let me try again

ErrorException
stripos() expects parameter 1 to be string, object given

Please or to participate in this conversation.