Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Lestah's avatar
Level 11

Trying to get property of non-object


public function viewProducts()
    {
        $products = Product::get();
        
        foreach($products as $key => $val){
            $category_name = Category::where(['id'=>$val->category_id])->first();
            $products[$key]->category_name = $category_name->name;
        }
        //echo "<pre>"; print_r($products); die;
        $products = json_decode(json_encode($products));
        return view('admin.products.view_products')->with(compact('products'));
    }

just want to display data on my table


<tbody>
                @foreach($products as $product)
                <tr {{-- class="table-info" --}}>
                    <td>{{ $product->id }}</td>
                    <td>{{ $product->category_id }}</td>
                    <td>{{ $product->category_name }}</td>
                    <td>{{ $product->product_name }}</td>
                    <td>{{ $product->product_code }}</td>
                    <td>{{ $product->product_color }}</td>
                    <td>{{ $product->url }}</td>
                    <td>{{ $product->price }}</td>
                    <td class="center">@if ($product->feature_item == 1) Yes @else No @endif</td>
                    <td>
                    @if(!empty($product->image))
                      <img src="{{ asset('admin-ui/images/products/small/'.$product->image) }}" style="width:60px;">
                    @endif
                  </td>
                    <td><a href="{{ url('/admin/edit-product/'.$product->id) }}" class="btn btn-outline-primary btn-sm">Edit</a> <a href="{{ url('/admin/add-attributes/'.$product->id) }}" class="btn btn-outline-primary btn-sm">Add</a> <a id="delProduct" rel="{{ $product->id}}" rel1="delete-product" href="javascript:" class="btn btn-outline-danger btn-sm deleteRecord" title="DeleteProduct">Delete</a></td>
                </tr>
                @endforeach
              </tbody>


0 likes
8 replies
Tray2's avatar

Can you show us the error message since it's very hard to say what is the non-object?

Snapey's avatar

What on earth are you doing in the controller?

Basically, instead of $products being a collection of $product objects youve turned it into an array

So all your arrows in the view would need to be changed to array syntax.

But really, fix the controller. every loop around the products will be performing a database query

Lestah's avatar
Level 11

@SNAPEY - yup i did try to use [' '] instead of the arrow still the same error it says trying to get the property of nonobject what i remember is i did $category_name = Category::where(['id'=>$val->category_id])->first(); inside the loop on my past project with the same concept it works fine because i need to get he category id and category name on my products table. on my category model it has a relation of has to many like this


<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    public function categories() 
    {
        return $this->hasMany('App\Category','parent_id');
    }
}

so i could display category id and category name on my products table

Snapey's avatar

Well start with your relationships and get them right. The snippet you showed is so that Category can have child categories - nothing to do with its relationship to products

Product will have categories() method

Category will have products() method

Implement these and test them using tinker to be sure they are working as you expect before writing more code.

Lestah's avatar
Level 11

i was able to display get rid of the error if i remove the


 foreach($products as $key => $val){
            $category_name = Category::where(['id'=>$val->category_id])->first();
            $products[$key]->category_name = $category_name->name;
        }

//and remove

<td>{{ $product->category_name }}</td>

but the problem is i wont be able to include the category name column on my table

Lestah's avatar
Level 11

Hey snapey thanks for responding just need to restart laravel its now working :D

Snapey's avatar

You should install Laravel debugbar so that you can see how many queries you are performing.

Your approach is all wrong at the moment

Snapey's avatar
    public function viewProducts()
    {
        $products = Product::with('category')->get();
       
        return view('admin.products.view_products')->with(compact('products'));
    }


// view

<tbody>
                @foreach($products as $product)
                <tr {{-- class="table-info" --}}>
                    <td>{{ $product->id }}</td>
                    <td>{{ $product->category->id }}</td>         <<<<<<
                    <td>{{ $product->category->name }}</td>.   <<<<<<
                    <td>{{ $product->product_name }}</td>
                    <td>{{ $product->product_code }}</td>
                    <td>{{ $product->product_color }}</td>
                    <td>{{ $product->url }}</td>
                    <td>{{ $product->price }}</td>
                    <td class="center">@if ($product->feature_item == 1) Yes @else No @endif</td>

Please or to participate in this conversation.