Pivot Table for 3 Laravel Tables

Asked

Viewed 112 times

0

My doubt would be how to display table data in a relationship Many to Many with more than two related tables:

Ex: Main table SALE PIVO VENDA_DETALHE table (where to buy/idproduct/idcentrocust) PRODUCT TABLE Table CENTROCUSTO


MODEL SALE

public function produto()
{
    return $this->belongsToMany(Produto::class,'venda_produto_centrocusto');
}

public function centrocusto()
{
    return $this->belongsToMany(CentroCusto::class,'venda_produto_centrocusto');
}

PRODUCT MODEL

public function venda()
{
    return $this->belongsToMany(Venda::class,'venda_produto_centrocusto');
}

MODEL COST CENTER

public function venda()
{
    return $this->belongsToMany(Venda::class,'venda_produto_centrocusto');
}

CONTROLLER public Function create() { $sales= Sale::with('product','centrocusto')->get();

return view('venda.pedido.create',compact('vendas'));
}

IN VIEW

foreach ($vendas as $venda) {
    foreach ($produtos as produto) {

         foreach ($produtos as produto) {
        // Aqui quero rodar os produtos e o centro de custo dessa venda;
         Esta dando erro aqui, ela esta trazendo todos os centros de custos

        }
    }
}

ERROR FIELD COST CENTER inserir a descrição da imagem aqui

  • He even took a look at the session Retrieving Intermediate Table Columns of documentation ?

  • I am reading, but I still could not implement, this giving error.

  • Then you could put the part of the code that is giving error in your question (only the part with error, the whole code does not. Please :D ). And try to explain what you have already tried and what error is showing. So there will be more people to help you.

  • To edit the question just click the edit button (it is only for you not to post your questions here in the comment)

1 answer

0

The ideal would be you show the error, but I will assume that the error is the way you make the relationship by view, and note that you have left to put the $ to declare the product.

foreach ($vendas as $venda) {
    foreach ($produtos as produto) {

Correct for this way, and see if your problem has been solved:

foreach ($vendas as $venda) {
    foreach ($venda->produto as $produto) {
  • I did so foreach ($sales as $sales) { $products = $sale->products; foreach ($products as $products) {

  • It is returning the products straight the problem and to return to the centrodecusto. When I give him a foreache he comes duplicate, if I try to access him straight from the dick.

  • Trying this way still comes duplicate? foreach ($sales the $sales) foreach ($sale->products the products) foreach ($product->centrocusto as $centrocusto) Edits your initial post and shows the error to make it easier to help.

  • I made this example using solicitation instead of selling. Cost Center is not within Product is within Request or Sale in case as well as product understood?

  • Tries to split the relation for several one for many, creates a new model and make the relation: Sell new hasMany Model. New hasMany product Model. Centrocusto hasMany newModel. newModel belongsTo For Sale. newModel belongsTo Product. newModel belongsTo Centrocusto. And do the foreach. gives a check in the documentation of one for many: https://laravel.com/docs/5.7/eloquent-relationships#one-to-Many

  • OK I’ll try, and put the result

Show 1 more comment

Browser other questions tagged

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