Doubt too much relationship for laravel

2

I have the following relationship:

My question comes in the programming of the order_products model, the real one would like to check if I'm doing it the right way, I programmed it like this:

class EncomendaProdutos extends Model
{
  use SoftDeletes;
  protected $fillable = [
    'CdEncomenda',
    'CdPessoa',
    'CdProduto',
    'CdPromo',
    'QtdProduto',
    'DescontoProdutos'
  ];

  protected $dates = ['deleted_at'];

  public function produto()
  {
    return $this->hasOne('App\Produto','CdProduto','CdProduto');

  }

  public function pessoa()
  {
    return $this->hasOne('App\Tipo_Produto','CdTipoProduto','CdTipoProduto');

 }

 public function promocao()
  {
    return $this->hasOne('App\Embalagem','CdEmbalagem','CdEmbalagem');

  }
 }

I want to be able to search for an order and it returns me all the products that have been ordered, as well as which person has placed the order, and whether it is linked to a promotion.

In the other models I did this:

Model Encomenda

class Encomenda extends Model
{
 use SoftDeletes;
 protected $fillable = [
    'CdPessoa',
    'Cidade',
    'Estado',
    'Bairro',
    'Rua',
    'Num',
    'Complemento',
    'CEP',
    'DescontoTotal',
    'VlTotal',
    'FlgStEncomenda',
    'FlgRepetirEntrega'
    ];
 protected $primaryKey = 'CdEncomenda';
 protected $dates = ['deleted_at'];

 public function cliente()
  {
    return $this->belongsTo('App\Pessoa','CdPessoa','CdPessoa');
  }

 public function encomenda_produtos(){
    return $this->belongsTo('App\EncomendaProdutos', 'CdEncomenda', 'CdEncomenda');
  }
}

And likewise in promocao .

That's right, my programming, this knowledge is not fixed in my mind.

An example usage:

  

I am a customer bought two product, so the tables should be salfas as follows:

Encomenda
CdEncomenda 1
outros dados da tabela

/////////
EncomendaProdutos
CdEncomenda 1
CdProtudo 10

////

CdEncomenda 1
CdProduto 23
    
asked by anonymous 22.10.2016 / 02:33

0 answers