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

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

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Decoding the "Read Property on Array" Error in Laravel Blade

The error Attempt to read property "name" on array is a classic symptom of a mismatch between your data structure and how you intend to iterate over it. This specific message originates from PHP, triggered when the Blade engine attempts to access an object property (like $item->name) on a variable that is currently just a raw array or a scalar value.

In the context of the provided example, this issue stems from a fundamental misunderstanding of how the Guzzle HTTP client returns data and how Laravel's compact function handles variables. The developer's view expects $data to be an array of objects where each item has a name property. However, the controller logic is passing the raw JSON response or a single decoded object directly to the view without verifying its structure.

Analyzing the Controller Logic

The controller code uses the Guzzle HTTP client to fetch data from the Lazada API:

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

The line $response->json() is the critical point of failure here. The json() method on a Guzzle response object attempts to decode the body as JSON and returns a PHP array. However, what is inside that array depends entirely on the API's response structure.

If the API returns a single product, $data becomes an array containing one element, or potentially a scalar if the root of the JSON is a simple string or number (though less likely for this endpoint). If the API returns a list of products, $data becomes a multidimensional array like ['product_1', 'product_2'].

The most common cause of the specific error reported is that $response->json() might be returning a scalar value (like a string) or the code expects $data to be an associative array where keys are strings, but the view treats it as an iterable collection of objects. Alternatively, if the API returns a JSON object like {"products": [...]}, and the code accesses $response->json()['products'], but fails to do so, $data becomes a single object, not an array. When you then loop with @foreach($data as $datas), Blade tries to access $datas->name. If $data was actually a string or a single object without a name property at the top level in the way expected, the error occurs.

Furthermore, if the API response is wrapped in an object like {"status": "success", "data": [...]}, assigning the whole result to $data means you are iterating over keys like "status" or "data". If $datas resolves to a string (the status message), accessing $datas->name throws the error because strings do not have named properties.

Inspecting the View and Data Structure

The Blade template attempts to render a table:

@foreach($data as $datas)
  <tr>
    <td>{{ $datas->name }}</td>
    <!-- other fields -->
  </tr>
@endforeach

This loop assumes $data is an array of objects. To fix this, you must inspect what $response->json() actually returns. Since the source material does not show a dd($data) statement, we cannot assume the API response format without guessing. However, based on standard API patterns, the response often looks like this:

{
  "status": "success",
  "data": [
    { "name": "Product A", ... },
    { "name": "Product B", ... }
  ]
}

If you assign the entire JSON object to $data, then inside your loop, $datas will be the key "status" (a string) in the first iteration. Accessing ->name on a string causes the fatal error.

The Solution: Normalizing the Data

The robust solution involves extracting the correct subset of data before passing it to the view. You should use collect() or array destructuring to ensure you are iterating over the actual product list, not the response wrapper.

Here is how you should modify the controller:

public function index()
{
    $response = Http::get('https://api.lazada.co.id/rest/products/get?filter=live&amp;app_key=100132&amp;sign_method=sha256&amp;timestamp=' . time() . '&amp;access_token=...&amp;sign=...');
    
    $json = $response->json();

    // Check if the response is wrapped in an object with a 'data' key
    // or if it is already an array.
    if (isset($json['data']) && is_array($json['data'])) {
        $items = collect($json['data'])->map(function ($item) {
            return (object)$item; 
        });
    } elseif (is_array($json)) {
        $items = collect($json)->map(function ($item) {
            return (object)$item;
        });
    } else {
        abort(404, 'Unexpected API response format');
    }

    return view('product', compact('items'));
}

In the updated view, you would then iterate over $items:

@foreach($items as $item)
  <td>{{ $item->name }}</td>
  <td>{{ $item->description }}</td> <!-- Note: typo in original source 'datas-description' -->
@endforeach

Key Pitfalls to Avoid

  1. Trust but Verify: Never assume the API response matches your model. Always inspect the structure of the raw JSON before processing it. Using dd($json) in a local environment is invaluable for debugging these types of issues.
  2. Variable Naming Consistency: The original view contains a typo: {{ $datas-description }}. This is invalid Blade syntax because you cannot use hyphens in property names within curly braces unless you are using array access syntax like $datas['description'] or if the property was named with underscores ($datas->description).
  3. Array vs Object: PHP allows accessing properties on arrays (e.g., ['name']), but Blade's {{ $var->property }} syntax specifically looks for object properties. If your loop variable is an array, you must switch to $datas['name'].

By ensuring the data passed to the view is a clean collection of objects and correcting the property access syntax in the template, the error will be resolved. This approach ensures that your Blade templates remain resilient against slight variations in API response structures.

Our network