简体   繁体   English

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

[英]using data from php post request (laravel)

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 : 发布请求返回以下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

Can anyone tell me what ive done wrong? 谁能告诉我我做错了什么?

Using json_decode 使用json_decode

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

If i attempt to use json_decode i get the following error: 如果我尝试使用json_decode,则会出现以下错误: 在此处输入图片说明

Update 更新资料

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! 我使它与json_decode一起使用是因为我试图获取根对象而不是对象的值! thank you for your help! 谢谢您的帮助!

It looks like $apiResult is a string, not an object. 看起来$apiResult是一个字符串,而不是一个对象。 You never decoded the JSON in your controller, just passed it on into the variable directly. 您从来没有在控制器中解码过JSON,而是直接将其传递到变量中。

Just inject the string directly into the Javascript and let your JS code get the properties it wants within that, ie just write 只需将字符串直接注入Javascript中,然后让您的JS代码在其中获取所需的属性即可,即只需编写

{{$apiResult}}

(I assume you're assigning the output of that as a JS variable.) (我假设您将其输出分配为JS变量。)

Alternatively, in your controller do 或者,在您的控制器中执行

'apiResult' => json_decode($result)

inside your return statement to convert it to an object. 在return语句中将其转换为对象。

Your $result is json. 您的$result是json。 which means it is string not object/array. 这意味着它不是对象/数组。 So you need to convert it. 因此,您需要对其进行转换。 Use json_decode() to convert json string. 使用json_decode()转换json字符串。 so after result add json decode statement 所以在结果之后添加json解码语句

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

you cannot display object or string on view you have to json_decode after json_decode you will get your object or array. 您无法在视图上显示对象或字符串,而必须先获得json_decode才能获取对象或数组。 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 那么你会得到objct或数组

use @foreach to loop through the data and display the value you want to display 使用@foreach遍历数据并显示要显示的值

@foreach(cols as col)
   display data
@endforeach

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 那是因为您正在尝试打印一个对象,请注意$apiResult->zipValues是一个对象,使用json_decode($result, true)将json转换为数组,然后使用Blade foreach打印所有值

controller 控制者

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

View 视图

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM