Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

noblemfd's avatar

ErrorException Undefined index: current_asset_position_name in Laravel API

I have this api as json in my Laravel-8 application:

public function index() {
    $client = new Client();
    $res = $client->request('GET','https://example.com/api/tracking/19SB22', [
       'query' => ['key' => 'jkkmkf']
   ])->getBody();
    $json = json_decode($res->getContents(), true); 
    $current = $json[0];
    $geo = explode(',', $json[0]['current_asset_position_coord']);        
    return view('welcome', [
        'current' => $current,
        'json' => $json,
        'geo' => $geo
    ]);

The view blade looks like this:

@foreach($json as $value)
<div class="cd-timeline__block js-cd-block">
  <div class="cd-timeline__img cd-timeline__img--movie js-cd-img">
    <img src="{{ asset('img/loc.svg') }}" alt="Picture">
  </div>
  <!-- cd-timeline__img -->
  <div class="cd-timeline__content js-cd-content">
    <h3>{{ $value['trip_status'] }}</h3>
    <p><b>{{ $value['current_asset_position_name'] }}</b></p>
    <a href="{{ route('tracking.details', $value['this_tracking_job_id']) }}" class="btn btn-primary btn-lg">View Details</a>
    <span class="cd-timeline__date" style="border: 2px solid green; background-color: green; border-radius: 12px; color:white;">{{ $value['Timestamp'] }}</span>
  </div>
  <!-- cd-timeline__content -->
</div>
@endforeach

I am trying to use it for Google Map.

When I rendered the page, I got this error:

ErrorException Undefined index: current_asset_position_name

and it highlights:

{{ $value['current_asset_position_name'] }}

The surprising thing thing is that the attribute is there in the API JSON:

dd($json) in the Controller gives:

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

It is there:

"current_asset_position_name" => "2, Adelaide, Australia"

How do I resolve this?

Thanks

0 likes
1 reply
Nakov's avatar
Nakov
Best Answer
Level 73

It is there on the first couple of iterations, but probably somewhere down the line it is missing. You can always fall back to a default value like this:

{{ $value['current_asset_position_name'] ?? '' }}

Please or to participate in this conversation.