如何打印Api响应数据作为集合

问题描述 投票:0回答:1

我试图将 API 响应数据打印为刀片上的集合,因为我使用了以下行

$customers =   collect(json_decode($response, true));

但是每当我尝试使用以下代码打印时:

 @foreach($customers as $row)
    <tr>
      <td>{{$row->first_name}} {{$row->last_name}}</td>
      <td>{{$row->email}}</td>
      <td>{{$row->phone}}</td>
      <td>{{$row->addresses['country_name']}}</td>
    </tr>
  @endforeach 

它显示以下错误,这里有什么问题?

尝试读取数组上的属性“first_name”

这是 API 响应:

Illuminate\Support\Collection {#341 ▼ // app\Http\Controllers\IntegrationController.php:61
  #items: array:1 [▼
    "customers" => array:3 [▼
      0 => array:27 [▼
        "id" => 6895839936762
        "email" => "[email protected]"
        "accepts_marketing" => false
        "created_at" => "2023-10-20T11:06:26-04:00"
        "updated_at" => "2023-10-20T11:06:26-04:00"
        "first_name" => "Russell"
        "last_name" => "Winfield"
        "orders_count" => 0
        "state" => "disabled"
        "total_spent" => "0.00"
        "last_order_id" => null
        "note" => "This customer is created with most available fields"
        "verified_email" => true
        "multipass_identifier" => null
        "tax_exempt" => false
        "tags" => "VIP"
        "last_order_name" => null
        "currency" => "USD"
        "phone" => "+16135550135"
        "addresses" => array:1 [▶]
        "accepts_marketing_updated_at" => "2023-10-20T11:06:26-04:00"
        "marketing_opt_in_level" => null
        "tax_exemptions" => []
        "email_marketing_consent" => array:3 [▶]
        "sms_marketing_consent" => array:4 [▶]
        "admin_graphql_api_id" => "gid://shopify/Customer/6895839936762"
        "default_address" => array:17 [▶]
      ]
      1 => array:26 [▶]
      2 => array:26 [▶]
    ]
  ]
  #escapeWhenCastingToString: false
}
php laravel laravel-5
1个回答
0
投票

就你而言,我认为你想要的正确的 foreach 是:

@foreach($customers[0]->customers as $row)
    <tr>
      <td>{{$row->first_name}} {{$row->last_name}}</td>
      <td>{{$row->email}}</td>
      <td>{{$row->phone}}</td>
      <td>{{$row->addresses['country_name']}}</td>
    </tr>
@endforeach
© www.soinside.com 2019 - 2024. All rights reserved.