2
I have the following relationship:
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
)?– novic
not the idea is to repeat the order, and he manages 2
– Renan Rodrigues
How you will control the generation of these numbers @Renanrodrigues?
– novic
I will make a foreach in m array of products and I will automatically save each one.
– Renan Rodrigues
I think I got it !!! if you have it
encomendas
andencomendas_produtos
theCdPessoa
only need to have in orders!– novic
I would do like that! ordering_productswith a primary key, and a key composed of orders and products, remove cdpessoa from ordering_products!
– novic
Exact, product orders only refers to ordered products
– Renan Rodrigues
Let’s go continue this discussion in chat.
– novic
Could you prepare an answer ? complete with relationships
– Renan Rodrigues
@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 ?
– Renan Rodrigues
your relationships
hasOne
should bebelongsTo
because it’s not inheritance you’re making is 1 for many. I didn’t answer anything in your question, because of that are manymodels
to tidy up, and at the time I went to observepromocoes
I ended up blurring the ideas, because it has categories, sub categories, in thought will be right. In Laravel also follows the3FN
and you don’t seem to be following this, so the question is still open.– novic
I apologize, I’m one of those who answer
Laravel
, but, your relationships I was very confused...– novic