-1

weird behavior or misunderstanding of Laravel relations. I have got a Product model. I getting products with translations and its category. I have defined relations in both models. I can access the data in the model or in the controller. but in view It says non-object.

Trying to get property 'slug' of non-object (View:

https://flareapp.io/share/LPdxbvvP - error.

enter image description here enter image description here enter image description here enter image description here

and here is a weird part. enter image description here as you can see here when I do display the category relation out using {{}}. it gives JSON string.

Where did I mess up?!

Murod
  • 22
  • 1
  • 6

1 Answers1

-1

guys, I am so sorry about this. I didn't think of the category relation data can be null. in that case, it does throw an error. The simple solution is this.

{{$item->category->slug ?? '-'}}
Murod
  • 22
  • 1
  • 6
  • why do you think so? the problem was with null data coming from relations. so this is the answer to what I asked for. – Murod Nov 04 '20 at 12:09