Doubts relationship too much for many

Asked

Viewed 96 times

2

I have the following relationship:

inserir a descrição da imagem aqui

My doubt comes in the programming of the ordering model:

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 intend to be able to research an order and it returns me all products that was ordered, besides knowing which person made the order, and even if it is linked to a promotion.

In other models I did so:

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 in the same way in promocao.

This certain my programming, this knowledge is not fixed in my mind.

An example of use:

I am 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
  • In that encomenda_produtos type I am customer I make the purchase of two products it will generate for each product a certain order (CdEncomenda)?

  • not the idea is to repeat the order, and he manages 2

  • How you will control the generation of these numbers @Renanrodrigues?

  • I will make a foreach in m array of products and I will automatically save each one.

  • I think I got it !!! if you have it encomendas and encomendas_produtos the CdPessoa only need to have in orders!

  • I would do like that! ordering_productswith a primary key, and a key composed of orders and products, remove cdpessoa from ordering_products!

  • Exact, product orders only refers to ordered products

  • Could you prepare an answer ? complete with relationships

  • @Virgilionovic asks me a question even within this question how would I access the methods within the order_products, as for example to get the name of the product ?

  • your relationships hasOne should be belongsTo because it’s not inheritance you’re making is 1 for many. I didn’t answer anything in your question, because of that are many models to tidy up, and at the time I went to observe promocoes I ended up blurring the ideas, because it has categories, sub categories, in thought will be right. In Laravel also follows the 3FN and you don’t seem to be following this, so the question is still open.

  • I apologize, I’m one of those who answer Laravel, but, your relationships I was very confused...

Show 7 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.