Lestah
194
8
Laravel

Trying to get property of non-object

Posted 3 months ago by Lestah

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>


Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.