Edelco
664
12
Laravel

Problem with returning a foreach loop from Laravel controller to blade view

Posted 1 year ago by Edelco

I am trying to do a foreach loop in a curL request in a method of a controller.

method (from StreamsController):

 public function getDataFromApi()
 {

$cong = Stream::pluck('name');

foreach($cong as $gemeente)
{
    $client = new Client([
        'headers'  => [
            'content-type' => 'application/json',
            'Accept' => 'application/json',
        ],
    ]);

    $response = $client->request('GET', 'http://xx.xx.xx.xx:xxxx/v2/servers/_defaultServer_/vhosts/_defaultVHost_/applications/'.$gemeente.'/instances/_definst_');

    $data = $response->getBody();
    $data = json_decode($data);
    $data = $data->incomingStreams;
}

return view('streams.api', compact('data'));
 }

My blade page:

 @extends('layouts.app')

 @section('content')
{{ dd($data) }}
@foreach($data as $result)
    <h3>{{ $result->name }}</h3>
@endforeach

 @endsection

But when I try to output the result in a blade view page doing a diedump it returns an empty array [] while I know for certain it should return at least ONE result of an active stream.

What am I doing wrong?

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.