Skip to content
Advertisement

Pivot Table with 3 Columns to get the value in Laravel 9

I am a new in Laravel and I am trying to get the value from third column id in pivot table

I have 3 tables and 4th table is pivot table, so my table structure is as follow

  1. TABLE Product Structure
JavaScript
  1. Table Attributes Structure
JavaScript
  1. Table Attribute Value Structure
JavaScript
  1. and I also make pivot table attribute_product
JavaScript

<<<<<<<<<<< MODELS OF TABLE >>>>>>>>>>>>>>>>>>>>>> 1.Table Product

JavaScript

2.Table Attribute

JavaScript
  1. Table Attribute Value
JavaScript

AND also I make a model of pivot table

JavaScript

<<<<<<<<<<<<<< DATA IN TABLES >>>>>>>>>>>>>>>>>>>>>>>>

  1. TABLE PRODUCT
JavaScript
  1. TABLE ATTRIBUTE
JavaScript
  1. TABLE ATTRIBUTE VALUES
JavaScript
  1. Pivot Table values attribute_product
JavaScript

Now I am using these commands in controller to find the value

JavaScript

when I try to get the value on base of attribute_value_id from pivot table its give me this error

ErrorException

Attempt to read property "value" on int

So how I can solve this problem.

Thanks

Advertisement

Answer

The pivot relation value should be a hasOne, not a belongsTo.

JavaScript

So in your foreach, you can now do:

JavaScript

Also try to keep your variable names straight. Don’t write $p->attributes as $value if $value is clearly an $attribute.

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