Tuesday, April 19, 2022

[FIXED] How to foreach and group like this on blade laravel?

Issue

I'm retrieving tracking data from the DHL API, as follows the data I get : Data API From DHL TRACKING

I use foreach in laravel blade, the result is like this: Foreach on Blade

How to make a foreach group where date in laravel blade like this : View on Website DHL

Please Help all, thank you..


Solution

You can group the result from API by date using Laravel's collection.

@php
    $groupedResult = collect([$apiResult])->groupBy(function($item) {
                      return Carbon::parse($item['timestamp'])->format('Y-m-d');
                  });
@endphp

@foreach($groupedResult as $date => $row)
 {{ $date }}
 @foreach($row as $item)
   // each day's transactions
 @endforeach
@endforeach

But I will suggest to process the data in controller, and then send to the front.



Answered By - Karim Naimy
Answer Checked By - Senaida (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.