You said "If I order them by created_at then the documents are displayed backwards" - what happens if you don't order them by created_at?
Elequent GroupBy Date and order by Asc inside the date group
Sorry, the thread I opened last week, I marked as answered, and can't un mark it. https://laracasts.com/discuss/channels/laravel/ordering-by-date-and-title
what is happening is that I call "latest()" which I then can group the newest images by date and have the latest ones show at the top. But, what I want to happen once they have been grouped is to order the images in "asc" order so that the images are in the correct order. If I order them by created_at then the documents are displayed backwards which isn't what I want.
After some testing over the past few days it turns out it hasn't worked.
Im not sure why but, while testing I added numbers to the image to make sure they were uploaded in order. It then saved the path and order in the database. From this I can call the path and retrieve the images from s3 storage and as I have the "order_by" column in the database I can use this to order it correctly.
My controller works to get the collection in the date order that I want. But as soon as I add "OrderBy('order', 'asc')" the date ordering is good, but the image order is completely off and random.
My Controller::
public function show(User $user)
{
$collection = CustomerScan::where('user_id', $user->id)
->latest()
->get()
->groupBy(function ($item) {
return $item->created_at->format('Y-m-d');
});
return view('admin.customers-profile.sections.scan-viewer.show')->with([
'collection' => $collection,
'user' => $user
]);
}
My View
@foreach ($collection as $date => $scans)
<div style="margin-top: 50px;">
@if(Carbon\Carbon::today()->toDateString() == $date)
<strong>TODAY</strong>
@elseif(Carbon\Carbon::today()->subDay()->toDateString() == $date)
<strong>YESTERDAY</strong>
@else
<strong>{{ Carbon\Carbon::parse($date)->format('jS F Y') }}</strong>
@endif
<small style="float: right">
{{ $scans->count() }} {{ Illuminate\Support\Str::plural('Scan',$scans->count()) }}
</small>
</div>
<hr style="border-top: 1px #ccc solid;">
<div class="row midsection">
{{-- @foreach($scans->chunk(3) as $chunk) --}}
@foreach($scans as $scan)
{{-- My template layout -- }}
@endforeach
{{-- @endforeach --}}
</div>
@endforeach
Any ideas.?
Many thanks
Please or to participate in this conversation.