I have the following post request in my controller:
public function CustomerCase($id)
{
$case = rental_request::with(['caseworker','user'])->where(['id'=>$id])->first();
$url = 'http://localhost:3000/api/priceByLocation';
$data = array('rentalObject' =>$case->attributesToArray());
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }
return view("case", ['case'=>$case, 'apiResult' => $result]);
}
The post request returns the following JSON:
Now in my view, I wish to access this data so what I did was the following:
@if($apiResult)
{{$apiResult->zipValues}}
@else
Not found
@endif
However here I get the following error:
Trying to get property of non-object
What have I done wrong?
Using json_decode:
return view("case", ['case'=>$case, 'apiResult' => json_decode($result)]);
If I attempt to use json_decode I get the following error:
Solution
I made it work with the json_decode - it was because I was trying to get the root object and not a value from the object.

