I'm trying to create models in Lumen based on my current database for a virtual store, but I'm having trouble setting up the relationships between them. So far I have the following Models:
* Product
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
final class Product extends Model
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'products';
public function productColor()
{
return $this->hasMany('ProductColor', 'id_product');
}
}
* ProductColor
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
final class ProductColor extends Model
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'product_colors';
public function sizeByProductColor()
{
return $this->hasMany('SizeByProductColor', 'id_product_color');
}
}
* SizeByProductColor
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
final class SizeByProductColor extends Model
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'sizes_by_product_color';
public function size()
{
return $this->hasMany('Size','id_size');
}
public function productColor()
{
return $this->belongsTo('productColor', 'id_product_color');
}
}
* Size
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
final class Size extends Model
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'sizes';
public function productColor()
{
return $this->belongsTo('SizeByProductColor');
}
}
My problem is that I've never worked with so many relationships and now it's giving me a lot of headaches. So, am I following the correct path as the Models? Note: Products have Colors and each product color has various sizes of footwear and stock. Example: If I needed to get the sizes for a given color I would use the following SQL:
SELECT * FROM sizes as s
LEFT JOIN product_colors as pc ON pc.id_color = 6
INNER JOIN sizes_by_product_color as sbpc ON s.id = sbpc.id_size AND pc.id = sbpc.id_product_color
How can I do this query on the Controller?