Relationship N: N in Laravel does not write object attributes

1

My tables briefly have the following structure:

minutes:

id | vigencia

Products:

id | nome | descricao

ata_producto:

ata_id | produto_id | vlr_produto | qtd_produto

The same product may have different value depending on the minutes, so the product registration is priceless. It is only set at the time that I enter it into a Minutes. For this I need to record the id of the product and the minutes, besides the quantity and value of each product.

The problem is there, I can relate the products to the minutes , but the price and quantity are not recorded.

Model - Product:

public function atas() {
    return $this->belongsToMany('App\Ata')->withPivot('vlr_produto', 'qtd_produto');
}

Model - Minutes:

public function produtos() {
    return $this->belongsToMany('App\Produto')->withPivot('vlr_produto', 'qtd_produto');
}

AtaController:

public function store(Request $request) {
    $input = $request->all();
    $produtos      = (array)array_get($input, 'produto_id');
    $quantidades   = (array)array_get($input, 'qtd_produto');
    $valores       = (array)array_get($input, 'vlr_produto');

    $ata = Ata::create($input);
    $ata->produtos()->sync($produtos, $quantidades, $valores);

    return redirect('admin/atas');
}

As I said, the relationship works. I do not know how to write, besides produto_id and ata_id , vlr_produto and qtd_produto .

Can anyone help me?

    
asked by anonymous 13.09.2015 / 20:23

1 answer

2

Laravel Eloquent provides basically two ways to work with several relationships for several (N: N).

The first is append and separate manually. For this use attach and detach .

The second is to build relationships between models with sync .

Using this method you should pass an array with the id and their respective values in the middle table.

$produto_id = 1;
$valor = 10;
$quantidade = 10;

$attributes = [ 'vlr_produto' => $valor, 'qtd_produto' => $quantidade ];

$ata->produtos()->sync( $produto_id => $attributes );

In case of several products:

$ata->produtos()->sync([ $produto_id => $attributes, 
                         $other_produto_id => $other_attributes ]);

ATTENTION

When you use the sync method:

  

Any IDs that are not in the given array will be removed from the intermediate table. So, after this operation is complete, only the IDs in the array will exist in the intermediate table

Laravel Eloquent Relationships

Apparently, you do not want to remove other links relationships with products when you invoke this method. So I recommend that you use attach to create relationships.

To do this, do:

$attributes = [ 'vlr_produto' => $valor, 'qtd_produto' => $quantidade ];

$ata->produtos()->attach( $produto_id, $attributes );

If you want to attach several products to ata , I suggest you loop

$produtos = [ 1, 2, 3 ];

$valores = [ 
   1 => 10, 
   2 => 20, 
   3 => 30 
];

$quantidades = [ 
   1 => 10, 
   2 => 20, 
   3 => 30 
];

foreach ( $produtos as $index => $produto ) 
{
  // atributos do produto atual
  $attributes = [ 
     'vlr_produto' => $valores[ $index ], 
     'qtd_produto' => $quantidades[ $index ] 
  ];

  $ata->produtos()->attach( $produto, $attributes );
}

This is a didactic solution. Adapt it according to your data.

Save the template at the end of the operation

$ata->save();
    
14.09.2015 / 03:34