Skip to content
Advertisement

Trying to get property ‘name’ of non-object (View: (…) resources/views/products/index.blade.php)

In table in view with data from products table I am traying to show names from user table but getting this error. I made Laravel relations in models and foreign keys. And added directory in product controller for user model.

Error: Trying to get property ‘name’ of non-object (View: /home/laravel/web/laravel.swt101.eu/public_html/abonamenty/resources/views/products/index.blade.php)

This is part of my controller for showing product data:

JavaScript

This is part of view where I am getting this error

JavaScript

This is rest code of this view:

JavaScript

This is my product model:

JavaScript

This is my user model:

JavaScript

Advertisement

Answer

The error indicates that $product->user is probably returning null. Maybe not all products are associated with users for some reason.

Find the product id and look it up in the database, to see if it has a user connected.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement