1
I’m doing a data insertion on a form
where it contains data of a relation many for many (Many to Many), my difficulty is in inserting in the pivot table more than one reference data, follow post below:
As we can see, I have the following array
:
- selected source list
- address_value
They have the id
of fornecedor
relating to the produto
and a value for each fornecedor
, if only to insert the id
of referencia
, I saw that it is done this way, example:
$insert = Product::create($data_form);
$providers = $insert->providers()->attach(1);
I would like to know how to send besides the id
, the value for that fornecedor
, follows below as this my model and as an example the data already in the database:
Model:
public function providers()
{
return $this->belongsToMany(Provider::class)
->withPivot(['value', 'created_at', 'updated_at']);
}
Bench:
According to the guidelines, I mentioned and encountered the following problem:
My new code tree:
$value_providers = array();
foreach ($data_form['lista_fornecedor_valor'] as $key => $value) {
$value_providers[] = number_format((float)$value, 2, '.', ',');
}
$providers = $insert->providers()->attach($data_form['lista_fornecedor_selecionado'], ['value' => $value_providers]);
returns me this error:
Array to string conversion (SQL: insert into `product_provider` (`created_at`, `product_id`, `provider_id`, `updated_at`, `value`) values (2017-10-14 20:50:19, 5782, 1, 2017-10-14 20:50:19, 20.00), (2017-10-14 20:50:19, 5782, 6, 2017-10-14 20:50:19, 20.00))
Another point is that in sql the 2 values are with the value of the 1 array, not sequential:
example:
$value_providers = ['20.00', '15.00']
Renan has to record item by item, so do a go and pass one by one
– novic
understood, codei this way and it worked, thanks for the help.
– Renne Galli
Do not fail to accept as an answer
– novic