1
My tables have, briefly, the following structure:
minutes:
id | vigencia
products:
id | nome | descricao
ata_produto:
ata_id | produto_id | vlr_produto | qtd_produto
The same product may have different value depending on the minutes, so the registration of the product has no price. It is only set at the time I insert it in a minutes.
For that I need to record the id
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 - Ata:
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 don’t know how to record, besides the produto_id
and ata_id
, the vlr_produto
and the qtd_produto
.
Can someone help me?
it is. using Sync(), even as you suggested, it didn’t work. I don’t know how to use the attach for it. I am looking for and will try.
– buback
I used $ata->products()->attach($products, $attributes); and it worked if I was only writing a single product, but as an array. It’s an error. Strange that the $products variable is also an array.
– buback
Using attach this way returns the error: "preg_replace(): Parameter Mismatch, Pattern is a string while Replacement is an array"
– buback
I’ll update the answer to case
attach
of various products– Alexandre Thebaldi
Show! It worked. I just fixed the foreach to use the index because it was giving error! Thank you so much for the help. : D
– buback