Relation between several tables in Eloquent

1

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?

    
asked by anonymous 10.07.2015 / 19:46

1 answer

0

in the product model

public function cores()
{
    return $this->hasMany('Cormodel');
}

//produto->cores me retorna todo as cores do produto

in the color model

public function produto()
{
    return $this->belongsTo('Produtomodel');
}

public function tamanhos()
{
    return $this->hasMany('Tamanhomodel');
}

//cor->tamanhos me retorna todos os tamanhos desse cor
//cores->produto me retorna o produto daquela cor

In model size

    public function cor()
    {
        return $this->belongsTo('Cormodel');
    }

    public function cores()
    {
        return $this->hasMany('Cormodel');
    }

//tamanho->cor me retorna o tamanho daquela cor
//tamanho->cores retorna as cores desse tamanho

and so on

    
10.07.2015 / 20:15