Hi. I've got an issue when trying to paginate a collection, but as its a collection there is no paginate.
I need to sort the data in a way that it orders it correctly and grabs the first in the group rather than show all the scans, is there a way I can put it into one so I can paginate ?
$collection = CustomerScan::where('user_id', Auth::user()->id)
->orderByRaw('CAST(created_at AS DATE) DESC') // order by future grouped by
->orderBy('group_id') // order within each group
->orderBy('order') // order within each group
->paginate(9);
$scans = $collection->groupBy('group_id')->map(function ( $scans ) {
return collect($scans)->first();
});
Sorry been a while since I was on this issue. Now i'm back on it.
In what sense do you mean put it in another query? Im not too good on the eloquent side but trying to learn.
This is what I have at the moment, but the map function is causing the issues.
$collection = CustomerScan::where('user_id', Auth::user()->id)
->orderByRaw('CAST(created_at AS DATE) DESC') // order by future grouped by
->orderBy('group_id') // order within each group
->orderBy('order')
->paginate($this->perPage);
$scans = $collection->groupBy('group_id')->map(function ( $scans ) {
return collect($scans)->first();
});
return view('livewire.scan-viewer.show-all-scans')->with(['scans' => $collection]);
View
@foreach($scans as $scan)
<x-scan-viewer-index-card :scan="$scan"></x-scan-viewer-index-card>
@endforeach