0

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:

enter image description here

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:

enter image description here

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.

1
  • @ADyson i have attempted to use json_decode however i get a new error Commented Jan 4, 2018 at 12:11

4 Answers 4

2

It looks like $apiResult is a string, not an object. You never decoded the JSON in your controller, just passed it on into the variable directly.

Just inject the string directly into the Javascript and let your JS code get the properties it wants within that, i.e. just write

{{$apiResult}}

(I assume you're assigning the output of that as a JS variable.)

Alternatively, in your controller do

'apiResult' => json_decode($result)

inside your return statement to convert it to an object.

Sign up to request clarification or add additional context in comments.

2 Comments

what did you do, out of interest? I think my first suggestion would get round the encoding issue.
i have updated my question basically i was attempting to get the root object and not a value from it
0

Your $result is json. which means it is string not object/array. So you need to convert it. Use json_decode() to convert json string. so after result add json decode statement

$result = file_get_contents($url, false, $context);
$result = json_decode($result);

Comments

0

you cannot display object or string on view you have to json_decode after json_decode you will get your object or array. note when you {{}} means the value is a string / number / boolean etc

in your controller

return view('your-view')
->withApiResult(json_decode($result);

then you will get objct or array

use @foreach to loop through the data and display the value you want to display

@foreach(cols as col)
   display data
@endforeach

Comments

0

That's because you are trying to print an object, note that $apiResult->zipValues is an object, use json_decode($result, true) to transform json to array and then print all values using blade foreach

controller

'apiResult' => json_decode($result, true)

View

@foreach($apiResult['zipValues'] as $value)
    {{ $value }}
@endforeach

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.