0

In my Laravel-5.8, I am trying to use Nested foreach statement in the view blade:

I have this controller:

public  function index()
{
    try{
    $client = new Client();
    $res = $client->request('GET','https://example.com/tracking/JKK998', [
       'query' => ['key' => 'jkkkm7725']
   ])->getBody();
    $geoLocation = json_decode($res->getContents(), true);
    $geoLocationData = collect($geoLocation)->sortBy(function ($data) {
        return Carbon::parse($data['current_timestamp'])->format('d-m-Y h:i:s');
    })->all(); 
    $currentLocation = collect($geoLocation)->sortByDesc(function ($data) {
        return Carbon::parse($data['current_timestamp'])->format('d-m-Y h:i:s');
    })->first(); 
    $currentLocationGroup = collect($geoLocation)->groupBy(function ($data) {
        return Carbon::parse($data['current_timestamp'])->format('d-m-Y');
    })->all();        
    $currentLocationFilter = explode(',', $currentLocation['current_coord'] ?? '');
    return view('index', [
        'currentLocation' => $currentLocation,
        'currentLocationGroup' => $currentLocationGroup,
        'currentLocationFilter' => $currentLocationFilter,
        'geoLocation' => $geoLocation,
        'geoLocationData' => $geoLocationData
    ]);
    }catch (Exception $exception) {
        Log::error($exception);
        return back();
    }
}

dd($currentLocationGroup);

gives:

array:14 [▼
  "22-06-2020" => Illuminate\Support\Collection {#297 ▼
    #items: array:25 [▼
      0 => array:30 [▼
        "current_timestamp" => "2020-06-22T11:53:29Z"
        "trip_status" => "DISPATCHED"
      ]
      1 => array:31 [▶]
      2 => array:31 [▶]
      ...

While dd($geoLocationData); gives:

array:554 [▼
 0 => array:30 [▼
  "current_timestamp" => "2021-01-22T12:30:25.4698261Z"
  "client_tracking_id" => "ddd3321"
  "current_asset_position_name" => "2, Adelaide, Australia"
  "trip_status" => "DISPATCHED"
]
1 => array:31 [▼
  "current_timestamp" => "2021-02-23T20:51:50.3066741Z"
  "client_tracking_id" => "TRIP-20200621007"
  "current_asset_position_name" => "20, Adelaide, Australia"
  "trip_status" => "AROUND CUSTOMER LOCATION"
]
....

View blade:

@foreach($currentLocationGroup as $groupValue)
<div class="time-label">
  <span class="bg-red">{{ \Carbon\Carbon::parse($groupValue['current_timestamp'])->format('d-m-Y') }}</span>
</div>
@foreach($geoLocationData as $value) 
 @if(\Carbon\Carbon::parse($groupValue['current_timestamp'])->format('d-m-Y') == \Carbon\Carbon::parse($value['current_timestamp'])->format('d-m-Y') )
<div>
  <i class="fas fa-truck bg-green"></i>
  <div class="timeline-item">
    <span class="time"><i class="fas fa-clock"></i> {{ \Carbon\Carbon::parse($value['current_timestamp'])->diffForHumans() ?? 'N/A' }}</span>
    <h3 class="timeline-header no-border">Trip Status: {{ $value['trip_status'] }} </h3>
    <div class="timeline-body">
      <p>Location: <b>{{ $value['current_name'] ?? '' }}</b></p>
      <p>Time: <b>{{ \Carbon\Carbon::parse($value['current_timestamp'])->format('d-m-Y h:i:sA') ?? 'N/A'  }}</b></p>
    </div>
  </div>
</div>
@endif 
endforeach

But I got this error:

ErrorException Undefined index: current_timestamp

C:\xampp\htdocs\tracking-app\vendor\laravel\framework\src\Illuminate\Collections\Collection.php:1414

public function offsetExists($key)
{
    return isset($this->items[$key]);
}

But it exists.

How do I get this resolved?

Thanks

mikefolu
  • 1,203
  • 6
  • 24
  • 57
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – miken32 Mar 19 '21 at 00:01
  • That `....` in your array dump is doing a lot of heavy lifting there. Obviously one of the array elements is missing the `current_timestamp` key. Check for it before you try using it. – miken32 Mar 19 '21 at 00:03
  • @miken32 - No it didn't solve the problem. The issue is with $currentLocationGroup. Because $geoLocationData is working fine when I removed $currentLocationGroup. from the view blade. Secondly, how do you mean by current_timestamp key. Kindly assist – mikefolu Mar 19 '21 at 00:12
  • Change this `\Carbon\Carbon::parse($groupValue['current_timestamp']) to this `$groupValue['current_timestamp']` and try in your view – Basharmal Mar 19 '21 at 02:53

0 Answers0