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

Published 7 months 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?

thomaskim
foreach($cong as $gemeente)
{
    ....
    $data = $data->incomingStreams;
    ...
}

On each loop, aren't you just erasing the previous $data and overriding it to be the latest incoming stream? Is it possible that the last incoming stream is just an empty array?

Edelco

@thomaskim : True. Didn't think of it that way. All I want is the complete list. I know when a stream is not connected it will return an empty array (in JSON) but I also know that when there IS an active stream it will return an array ($data) where I want to get the "incomingStreams".

Cronix
Cronix
7 months ago (649,570 XP)
$decoded = json_decode($response->getBody());

// add to data array instead of overwriting
$data['incomingStreams'][] = $decoded->incomingStreams;
@foreach ($incomingStreams as $stream)
  {{ $stream->name }}
@endforeach 

Edit: fixed typo

Edelco

@Cronix

     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_');

        $decoded = json_decode($response->getBody());

        // add to data array instead of overwriting
        $data['incomingStreams'][] => $decoded->incomingStreams;
    }
    
    return view('streams.api', compact('data'));
     }

Gives me an error:

 (1/1) FatalThrowableError
 Parse error: syntax error, unexpected => (T_DOUBLE_ARROW)

 in StreamsController.php (line 51)
Cronix
Cronix
7 months ago (649,570 XP)

Sorry, typo. Should have been an = instead of a =>

Edelco

@Cronix

Still an error:

 (2/2) ErrorException
 Undefined variable: incomingStreams (View: 

/home/vagrant/sites/kerkdienstuitzending.nl/resources/views/streams/api.blade.php)

Cronix
Cronix
7 months ago (649,570 XP)

return view('streams.api', $data);

Edelco

@Cronix Thanks for your effort. Still error:

 (2/2) ErrorException
Trying to get property of non-object (View: 

/home/vagrant/sites/kerkdienstuitzending.nl/resources/views/streams/api.blade.php)

Cronix
Cronix
7 months ago (649,570 XP)

Cant really help you on that as I don't have your data. Try doing a dd($data) just before loading the view and see what it holds. You should be able to troubleshoot it from there. I was going off your original example.

Edelco

@Cronix Did a dd($data) and seems that it is not returning $data to my blade view:

 Undefined variable: data
Cronix
Cronix
7 months ago (649,570 XP)

Most of this is basic php stuff. So, define $data before your loop.

$data = [];

foreach($cong as $gemeente)
Edelco

@Cronix I fixed it with using:

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

The dd($data) now shows all API request. Thanks a lot !

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