Jay240692
Jay240692

Reputation: 176

Passing API data to view Laravel

I can't figure out why my data is not being passed to View I get the message Undefined variable: project

Can someone help identify where i am going wrong?

when i return $project->name straight from the controller it displays the response, however fails to pass to view.

Here is my controller function:

public function getApi(){

   $client = new Client();
     $response = $client->get('https://play.geokey.org.uk/api/projects/77');

        $body = $response->getBody()->getContents();
         $project = json_decode($body);

    return view ('response', compact($project));


}

And here is my View:

<h1>Welcome</h1>

<table class="table table-bordered">
    <tr>
        <th>Project Name</th>
        <th>Date of Creation</th>
        <th>Contribution Total</th>
    </tr>

    <tr>
        <td>{{$project->name}}</td>
    </tr>


</table>

Upvotes: 3

Views: 5142

Answers (2)

Gravy
Gravy

Reputation: 12445

return view ('response', compact('project')); instead of compact($project)

Upvotes: 1

Bartosz
Bartosz

Reputation: 388

Instead of:

return view ('response', compact($project));

Type

return view ('response', compact('project'));

Upvotes: 7

Related Questions