Home > Software engineering >  How to foreach and group like this on blade laravel?
How to foreach and group like this on blade laravel?

Time:04-08

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..

CodePudding user response:

Me personally I'd do that all in the controller. Hit the api with only the info you need (looks like there is more there than you actually need), process that data in the controller and build your own collection and do sorting and grouping from that data and then send to the front. Keep your logic in the controllers.

CodePudding user response:

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.

  • Related