Can you show us the error message since it's very hard to say what is the non-object?
Mar 9, 2019
8
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>
Please or to participate in this conversation.