How to achieve similar functionality using foreach and group in Laravel's Blade template?
P粉106301763
P粉106301763 2024-02-25 16:56:20
0
2
410

I am retrieving tracking data from DHL API and the data I am getting is as follows: Data from DHL Tracking API

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

on the blade

How to create a foreach group in Laravel Blade as follows: View on DHL website

Please help, thank you.

P粉106301763
P粉106301763

reply all(2)
P粉237125700

Personally, I would do it all in the controller. Access the api with only the information you need (which looks like more information than you actually need), process that data in the controller and build your own collection, sort and group that data, and send to the frontend. Keep the logic in the controller.

P粉715228019

You can use Laravel's collections to group results from the API by date.

@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 recommend processing the data in the controller and then sending it to the front end.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!