Support the ongoing development of Laravel.io →
Database Laravel.io

I have three tables:

  1. attributes id name description
  2. attributes_values id attribute_id value
  3. product_attributes id product_id attribute_value_id

I use one controller (CategoryController.php) and my three models are Attribute,AttributeValues and ProductAttribute

My controller:

$all_attributes = Attribute::with('attribute_values')->with('product_attribute')->get()->toArray();

My models are:

Attribute:

public function attribute_values(){
        return $this->hasMany('AttributeValues');
    }
    public function product_attribute(){
        return $this->hasMany('ProductAttribute');
   }

AttributeValues:

public function attribute(){
        return $this->belongsTo('Attribute'); 
    }

ProductAttribute:

public function product(){
            return $this->belongsTo('Product');
        }
Last updated 2 years ago.
0

Generate a pivot table

php artisan generate:pivot tableone tabletwo

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.