dwigt
dwigt

Reputation: 611

Pass variable from PHP response to blade view object

While trying to request data from en external API, I want to control how the response is being passed to my view or database. However what would be the correct way to write the code below, so instead of simply echoing the data onto the view I would like to store it inside an object that I can pass to my view or model in a more controlled way?

public function index()
    {
        $contents = $this->saveApiData();
        return View::make('stats.index')->with('contents', $contents);
    }

     public function saveApiData()
    {
        $client = new Client(['base_uri' => 'https://owapi.net/api/v3/u/']);
        $res = $client->request('GET', "data" . "/blob");
        echo $res->getStatusCode();
        echo $res->getBody();
    }

Upvotes: 0

Views: 628

Answers (1)

Sandeesh
Sandeesh

Reputation: 11916

Just put them together in an array and return it. You never echo data in a function to return them.

public function saveApiData()
{
    $client = new Client(['base_uri' => 'https://owapi.net/api/v3/u/']);
    $res = $client->request('GET', "data" . "/blob");

    $contents = [
        'status' => $res->getStatusCode(),
        'body' => $res->getBody()
    ];

    return $contents;
}

Upvotes: 1

Related Questions