1
I have this parameter to import data via CSV and I want to know how to create the UPDATE function based on the CODE field.
public function importEstoque(Request $request)
{
$validator = Validator::make($request->all(), [
'file' => 'required'
]);
if ($validator->fails()) {
return redirect()
->back()
->withErrors($validator);
}
$file = $request->file('file');
$csvData = file_get_contents($file);
$rows = array_map("str_getcsv", explode("\n", $csvData));
$header = array_shift($rows);
foreach ($rows as $row) {
$row = array_combine($header, $row);
Estoque::insert([
'codigo' => $row['Código'],
'nome' => $row['Produto'],
'estoque' => $row['Qtd.'],
]);
}
}
I didn’t quite understand the question I could explain better ?
– Bulfaitelo
If that’s what I’m thinking, you want to do another method to update, and update based on code ?
– Bulfaitelo
In the box it would not be for the ID, but for the CODE field that in this application represents the product’s barcode. Then the user will import a file with the Barcode, Product and Quantity, with this, update at the base. It is possible to do this?
– Aguinaldo Tupy
In that same important function ?
– Bulfaitelo
Could be a new function, I put it as an example.
– Aguinaldo Tupy
You can do one that updates and inserts at the same time, but I will create one just to update based on this import you made
– Bulfaitelo
Perfect. Thanks for the support/
– Aguinaldo Tupy
Try it on and tell me.
– Bulfaitelo