ErrorException (E_NOTICE) Undefined variable: sheet

0

I get this error in Laravel when wanting to import an excel document into my database in mysql the code is as follows:

 public function store(ImportadorMarceloRequest $request)
    {
        //
        $path = $request->file('archivo')->storeAs('uploads/importaciones', $request->file('archivo')->getClientOriginalName(), 'web');
        $filePath = public_path() .'/'.$path;
        Excel::load($filePath, function($reader) use ($request){
            $objExcel = $reader->getExcel();
            $sheet = $objExcel->getSheet(0);

            DB::transaction(function(){
                foreach ($sheet->toArray() as $key => $row) {
                    if ($key >= 3) {
                        try {
                            $inmueble = Inmueble::where('id_unico', $row[1])
                                            ->where('promocion', $row[2])
                                            ->where('id_prinex', $row[3])->first();

                            if ($inmueble == null) {
                                $inmueble = new Inmueble();
                            }
                            $inmueble->fecha_datos = empty(trim($row[0])) ? null : $row[0];
                            $inmueble->id_unico = empty(trim($row[1])) ? null : $row[1];
    
asked by jose antonio palomo 30.07.2018 в 13:58
source

1 answer

1

The anonymous function within DB :: transaction should include a use ($sheet) . A closure does not have access to the variables defined outside

$sheet = $objExcel->getSheet(0);
DB::transaction(function() use ($sheet) {
 ...
});
    
answered by 30.07.2018 в 14:45