Hello @martinbean I am having some trouble with the relationships, hoping you can give me a few pointers...
Here are the tables
products
+------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| url | varchar(255) | NO | | NULL | |
| enabled | tinyint(1) | NO | | NULL | |
| tenant_id | bigint(20) unsigned | YES | MUL | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+------------+---------------------+------+-----+---------+----------------+
skus
+------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| tenant_id | bigint(20) unsigned | YES | MUL | NULL | |
| product_id | bigint(20) unsigned | NO | MUL | NULL | |
| sku | varchar(255) | NO | | NULL | |
| name | varchar(255) | NO | | NULL | |
| price | decimal(8,2) | YES | | NULL | |
| stock | int(11) | YES | | NULL | |
| enabled | tinyint(1) | NO | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+------------+---------------------+------+-----+---------+----------------+
attributes
+------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| tenant_id | bigint(20) unsigned | YES | MUL | NULL | |
| name | varchar(255) | NO | | NULL | |
| enabled | tinyint(1) | NO | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+------------+---------------------+------+-----+---------+----------------+
attribute_sku
+--------------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------------+------+-----+---------+-------+
| attribute_id | bigint(20) unsigned | NO | PRI | NULL | |
| sku_id | bigint(20) unsigned | NO | PRI | NULL | |
| value | varchar(255) | NO | | NULL | |
+--------------+---------------------+------+-----+---------+-------+
Then in my models, I have..
Product
With the following relationships
public function sku()
{
return $this->hasMany(Sku::class);
}
public function attribute()
{
return $this->hasManyThrough(Attribute::class, Sku::class);
}
Sku
With the following relationships
public function product()
{
return $this->belongsTo(Product::class);
}
public function attribute()
{
return $this->hasMany(Attribute::class)->withPivot('value');
}
And finally Attribute
With the following relationships
public function sku()
{
return $this->belongsToMany(Sku::class);
}
public function product()
{
return $this->belongsToMany(Product::class);
}
However, when I try and load the attributes off the Product
$product->load('attribute');
I get the following error
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'attributes.sku_id' in 'on clause' (SQL: select `attributes`.*, `skus`.`product_id` as `laravel_through_key` from `attributes` inner join `skus` on `skus`.`id` = `attributes`.`sku_id` where `skus`.`product_id` in (1) and `attributes`.`tenant_id` is null)
What I want to do is get a list of all the Attributes (Name and Value) for a given Product. I guess there are some relationship issues.