【问题标题】:Attempt to read property "name" on array (View: C:\xampp\htdocs\Testing\resources\views\product.blade.php)尝试读取数组上的属性“名称”(查看:C:\xampp\htdocs\Testing\resources\views\product.blade.php)
【发布时间】:2021-11-26 13:57:28
【问题描述】:

我是 laravel 的初学者,当我将统计表放入刀片时,我收到了这个警告

尝试读取数组上的属性“名称”(查看:C:\xampp\htdocs\Testing\resources\views\product.blade.php)

这是控制器

public function index()
{
    $response = Http::get('https://api.lazada.co.id/rest/products/get?filter=live&app_key=100132&sign_method=sha256&timestamp=1612318435886&access_token=50000801006o5nrcA5192d1f9ag1FHQBUqffCEyCmrXDohvhzExSkczUnnxJ4y&sign=F31584775544970D59AB58EC4B1B77933BC2D32401E33C1D2D5095690C31627C');
    $data = $response->json();
    return view('product',compact ('data'));
}

这是视图:

@extends('layout/main')

@section ('title', 'Testing Laravel')
@section ('container')

<div class="container">
  <div class="row">
    <div class="col-10">
      <h1 class="mt-3">List Product</h1>
      <table class="table">
        <thead class="thead-dark">
          <tr>
            <th scope="col">#</th>
            <th scope="col">Name</th>
            <th scope="col">Desc</th>
            <th scope="col">Brand</th>
            <th scope="col">Clothing Material</th>
            <th scope="col">Leather Material</th>
          </tr>
        </thead>
        <tbody> 
        @foreach($data as $datas)
          <tr>
            <th scope="row">1</th>
            <td>{{ $datas->name }}</td>
            <td>{{ $datas-description }}</td>
            <td>{{ $datas->brand }}<td>
            <td>{{ $datas->clothing_material }}</td>
            <td>{{ $datas->leather_material }}</td>
          </tr>
        @endforeach
        </tbody>
      </table>
    </div>
  </div>
</div>
 
@endsection

【问题讨论】:

  • Welcome to SO ...你的错误信息很清楚Attempt to read property "name" on array
  • 您尝试过什么来解决您的问题?你被困在哪里了?您是否尝试转储 $datas 以查看其中包含的内容?

标签: php laravel


【解决方案1】:

根据错误消息,它清楚地显示Attempt to read property (because you are trying to access like $data-&gt;name &lt;--OBJECT) on array 并且您的 foreach 变量是数组。所以你需要访问类似的键值对。

在循环中这样使用-

     {{ $data['name'] }}
     {{ $data['description'] }}

【讨论】:

  • 请在您的答案中添加一些解释,以便其他人可以从中学习
猜你喜欢
  • 2020-04-16
  • 2020-05-23
  • 2019-11-16
  • 2018-03-22
  • 2019-02-24
  • 2020-09-26
  • 2019-04-08
  • 2020-10-25
  • 1970-01-01
相关资源
最近更新 更多