Attempt to read property "name" on array (View: C:\xampp\htdocs\Testing\resources\views\product.blade.php)

Viewed 9242

I'm beginner in laravel, when I put stats table in blade I got this warning

Attempt to read property "name" on array (View: C:\xampp\htdocs\Testing\resources\views\product.blade.php)

This is Controller

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'));
}

This is view:

@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
1 Answers

According to the error message its clearly saying Attempt to read property (because you are trying to access like $data->name <--OBJECT) on array and your foreach variable is array. so you will need to access like key value pair.

Use like this in loop-

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