使用来自php post请求(laravel)的数据

使用来自php post请求(laravel)的数据,php,json,laravel,Php,Json,Laravel,我的控制器中有以下post请求: public function CustomerCase($id) { $case = rental_request::with(['caseworker','user'])->where(['id'=>$id])->first(); $url = 'http://localhost:3000/api/priceByLocation'; $data = array('rentalObject' =>$

我的控制器中有以下post请求:

    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]);
}
post请求返回以下
JSON

现在,在我看来,我希望访问这些数据,因此我所做的是:

@if($apiResult)
          {{$apiResult->zipValues}}
@else
      Not found
@endif
但是,在这里我得到了以下错误:

Trying to get property of non-object
谁能告诉我我做错了什么

使用json_解码

return view("case", ['case'=>$case, 'apiResult' => json_decode($result)]);
如果尝试使用json_解码,则会出现以下错误:

更新

return view("case", ['case'=>$case, 'apiResult' => json_decode($result)]);

我使用了
json_decode
这是因为我试图获取根对象,而不是对象的值!谢谢你的帮助

您的
$result
是json。这意味着它是字符串而不是对象/数组。所以你需要转换它。使用
json\u decode()
转换json字符串。所以在结果之后添加json解码语句

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

您的
$result
是json。这意味着它是字符串而不是对象/数组。所以你需要转换它。使用
json\u decode()
转换json字符串。所以在结果之后添加json解码语句

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

它看起来像是一个字符串,而不是一个对象。您从未在控制器中解码JSON,只是将其直接传递到变量中

只需将字符串直接注入Javascript,让您的JS代码获得它想要的属性,即只需编写

{{$apiResult}}
(我假设您将其输出指定为JS变量。)

或者,在控制器中执行以下操作:

'apiResult' => json_decode($result)

在return语句中,将其转换为对象。

它看起来像是一个字符串,而不是对象。您从未在控制器中解码JSON,只是将其直接传递到变量中

只需将字符串直接注入Javascript,让您的JS代码获得它想要的属性,即只需编写

{{$apiResult}}
(我假设您将其输出指定为JS变量。)

或者,在控制器中执行以下操作:

'apiResult' => json_decode($result)

在return语句中,将其转换为对象。

您不能在视图中显示对象或字符串,必须进行json\u解码 在json_解码后,您将获得您的对象或数组。 注意{{}表示值为字符串/数字/布尔值等

在控制器中

return view('your-view')
->withApiResult(json_decode($result);
然后您将得到objct或数组

使用@foreach循环遍历数据并显示要显示的值

@foreach(cols as col)
   display data
@endforeach

您不能在必须解码的视图上显示对象或字符串 在json_解码后,您将获得您的对象或数组。 注意{{}表示值为字符串/数字/布尔值等

在控制器中

return view('your-view')
->withApiResult(json_decode($result);
然后您将得到objct或数组

使用@foreach循环遍历数据并显示要显示的值

@foreach(cols as col)
   display data
@endforeach

这是因为您试图打印一个对象,请注意,
$apireult->zipValues
是一个对象,请使用
json\u decode($result,true)
将json转换为数组,然后使用blade foreach打印所有值

控制器

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

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

这是因为您试图打印一个对象,请注意,
$apireult->zipValues
是一个对象,请使用
json\u decode($result,true)
将json转换为数组,然后使用blade foreach打印所有值

控制器

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

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

@ADyson我尝试使用json_解码,但是我得到了一个新的error@ADyson我曾尝试使用json_解码,但出现了一个新错误出于兴趣,你做了什么?我想我的第一个建议可以绕过编码问题。我已经更新了我的问题,基本上我是在尝试获取根对象,而不是它的值。出于兴趣,你做了什么?我想我的第一个建议可以绕过编码问题。我已经更新了我的问题,基本上我是想得到根对象,而不是它的值