Hello , I am trying to enter some data through a file excel
but I need it to be imported from a specific sheet, I was consulting the documentation of laravel excel 3.1
but I do not understand very well how to do it, I have the following:
controller import:
class Import implements WithMultipleSheets
{
public function sheets(): array
{
return [
'importar' => new InstrumentosImport(),
'importar' => new CalibracionImport(),
];
}
}
InstrumentsImport (driver)
class InstrumentosImport implements ToModel,WithHeadingRow
{
public function model(array $row)
{
$tipo = tbl_tipo_instrumento::firstOrCreate([
'tipo_nombre' => $row['tipoinstrumento']
]);
//dd($row);
//dd($row['tipo']); //ver si el dato llega
//Si no encuentra el dato en la tabla lo crea
/*$tipo = tbl_tipo_instrumento::firstOrCreate(
['tipo_nombre' => $row['tipoinstrumento']],//busca
['tipo_nombre'=>$row['tipoinstrumento']] //crea
);*/
$marca = tbl_marca::firstOrCreate(
['mar_nombre' => $row['marca']],
['mar_nombre'=>$row['marca']]
);
$id = Auth::id(); //trae el id del usuario logeado
return new tbl_instrumentos([
'ins_codigo' => $row['codigo'],
'ins_observacionInicial' => $row['observacioninicial'],
'ins_proximaCalibracion'=>$row['proximacalibracion'],
'ins_fechaUltimaCalibracion'=>$row['ultimacalibracion'],
'ins_claseOexactitud' => $row['claseoexactitud'],
'ins_nSerie' => $row['nserie'],
'ins_divOescala'=>$row['divoescala'],
'ins_consecutivoInterno' => $row['consecutivointerno'],
'ins_observaciones' =>$row['observaciones'],
'ins_observacionFinal' => $row['observacionfinal'],
'ins_magnitud' => $row['magnitud'],
'ins_modelo' => $row['modelo'],
'ins_codActividad' => $row['codigoactividad'],
'ins_area' => 'null',
'ins_usu_id' => $id,
'ins_tipo_id' => $tipo->tipo_id,
'ins_mar_id' => $marca->mar_id,
]);
}
public function headingRow(): int
{
return 5;
}
It is not very clear to me if the sheets function should be put in the part of my import instruments or where I leave it is fine, or if I have to make a change in this line that I have in the main controller
Excel::import(new InstrumentosImport,$request->import_file);
Multiple Sheets
When a file has multiple sheets, each sheet will go through the import object. If you want to handle each sheet separately, you'll need to implement the concern
WithMultipleSheets
.The method
sheets()
expects an array of sheet import objects to be returned.If you only know the worksheet name, you can use the worksheet name as a selector. Put the name of the worksheet as the array index to link that worksheet to its sheet import object.
So if your sheet is called
importar
, you can do the following.A sheet import class can import the same concerns as a normal import object.
So if your class is called
InstrumentosImport
, there you put the functionmodel
in which you insert the data.Finally, in the Controller you call the main class , which I've renamed here to
ImportarExcel
avoid ambiguity with some other class that might also be called Import.