简体   繁体   中英

How to foreach and group like this on blade laravel?

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM