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
    Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->foreignId('category_id')->constrained();
            $table->timestamps();
   });

  1. Table Attributes Structure
   Schema::create('attributes', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamps();
        });
  1. Table Attribute Value Structure
 Schema::create('attribute_values', function (Blueprint $table) {
            $table->id();
            $table->string('value');
            $table->foreignId('attribute_id')->constrained();
            $table->timestamps();
        });
  1. and I also make pivot table attribute_product
 Schema::create('attribute_product', function (Blueprint $table) {
            $table->foreignId('product_id')->constrained();
            $table->foreignId('attribute_id')->constrained();
            $table->foreignId('attribute_value_id')->constrained();
        });

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

class Product extends Model
{
    use HasFactory;


public function attributes()
    {
        return $this->belongsToMany(Attribute::class)->using(AttributeProduct::class)->withPivot('attribute_value_id');
    }
    
 }

2.Table Attribute

class Attribute extends Model
{
    use HasFactory;


    public function products()
    {
        return $this->belongsToMany(Product::class)->using(AttributeProduct::class)->withPivot('attribute_value_id');
    }

}

  1. Table Attribute Value
class AttributeValue extends Model
{
    use HasFactory;

    protected $guarded = [];

    public function attribute_product()
    {
        return $this->hasMany(AttributeProduct::class, 'attribute_value_id');
    }


 }

AND also I make a model of pivot table

use IlluminateDatabaseEloquentRelationsPivot;

class AttributeProduct extends Pivot
{
    use HasFactory;

    public function value()
    {
        return $this->belongsTo(AttributeValue::class,'attribute_value_id');
    }
}

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

  1. TABLE PRODUCT
ID   TITLE           category_id
6    Samsung S22        2
  1. TABLE ATTRIBUTE
ID    NAME
1     SIZE
2     COLOR
  1. TABLE ATTRIBUTE VALUES
ID   VALUE     attribute_id
1     SM           1
2      M           1
3     RED          2
4     BLUE         2
  1. Pivot Table values attribute_product
product_id   attribute_id   attribute_value_id 
  6              1                 1
  6              2                 4

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

$p = Product::find(6);

foreach($p->attributes as $value){
   echo $value->name." = ". $value->pivot->attribute_value_id->value."<br>";
}

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.

class AttributeProduct extends Pivot
{
    use HasFactory;

    public function value()
    {
        return $this->hasOne(AttributeValue::class, 'id', 'attribute_value_id');
    }
}

So in your foreach, you can now do:

foreach($p->attributes as $attribute){
   echo $attribute->name." = ". $attribute->pivot->value->value."<br>";
}

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