【问题标题】:How to pass variable from Guzzle Request In Laravel如何在 Laravel 中从 Guzzle 请求中传递变量
【发布时间】:2018-01-23 12:35:09
【问题描述】:

我正在尝试将 guzzle 请求中的变量传递给我的视图。

这是我的控制器

  $client = new Client();
  $res = $client->request('GET', 'https://api.iugu.com/v1/customers?api_token=secret');
  $result = $res->getBody();
  $clientes = json_decode($result, true);
  return view('sections.client.index')->with('clients', $clientes['items']);

但返回错误:

Trying to get property of non-object (View: /var/www/html/cron_verify/resources/views/sections/client/index.blade.php)

这是我的 JSON

这是我的观点

@extends('layouts.app')
@section('content')
<div class="container">
  <div class="row">
    <div class="col-md-12 col-md-3-offset table_box">
      <table class="table">
        <tr>
          <th>Nome</th>
          <th>Status</th>
          <th>Faturas</th>
          <th>Situação</th>
        </tr>
        @foreach($clients as $value)
        <tr>
          <td>{{$value->name}}</td>
          <td>{{$value->email}}</td>
          <td>{{$value->number}}</td>
        </tr>
        @endforeach
      </table>
    </div>
  </div>
</div>
@endsection

我不明白为什么会收到此错误,因为该值在 JSON 响应中。是什么导致了错误?

【问题讨论】:

  • 尝试dd($clientes) 并告诉我们并尝试添加带有查看代码的完整错误消息
  • @Vinicius Rosa 请同时显示您的 html 代码
  • @Sohel0415 我编辑了我的问题。
  • @Kirit 我编辑了我的问题
  • 请也显示您的查看代码

标签: php json laravel


【解决方案1】:

您需要将access 元素作为array 值。

@foreach($clients as $value)
    <tr>
      <td>{{$value["name"]}}</td>
      <td>{{$value["email"]}}</td>
      <td>{{$value["number"]}}</td>
    </tr>
@endforeach

【讨论】:

  • 像魅力一样工作!非常感谢@Sohel0415
【解决方案2】:

正如错误所说,您正在使用数组,而不是对象。请改用这个 foreach。

    @foreach($clients as $value)
    <tr>
      <td>{{$value['name']}}</td>
      <td>{{$value['email']}</td>
      <td>{{$value['number']}}</td>
    </tr>
    @endforeach

【讨论】:

    【解决方案3】:

    您错误地访问了数组

    试试:

    @foreach($clients as $value)
        <tr>
          <td> {{$value["name"]}} </td>
          <td> {{$value["email"]}} </td>
          <td> {{$value["number"]}} </td>
        </tr>
    @endforeach
    

    【讨论】:

      【解决方案4】:

      这是因为您正在使用 $clientes = json_decode($result, true); 返回一个关联对象,而在您看来,您必须使用它:

      <tr>
            <td>{{$value['name']}}</td>
            <td>{{$value['email']}}</td>
            <td>{{$value['number']}}</td>
          </tr> 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-26
        • 2017-12-11
        • 2018-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多