Home > Software engineering >  How to use foreach with external API using Http in laravel?
How to use foreach with external API using Http in laravel?

Time:08-01

I am trying to display data from external API using Http

web.php:

Route::resource('job', 'App\Http\Controllers\AjaxController');

Controller:

public function index()
{
    
    $apis = Http::get("example/api/records");
    return view("job.index", [
        "apis" => json_decode($apis)
    ]);
}

view:

@foreach ($apis as $item)
<h1>{{$item->DocKey}}</h1>
@endforeach

I am getting the data from the api inside the view as showing below:

enter image description here

but i get this error:

Undefined property: stdClass::$DocKey

what seems to be the problem here?

CodePudding user response:

Your controller should look like:

$response = Http::get('http://yourdomain.com/example/api/records');
$apis = json_decode($response->body());
  • Related