Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.3k views
in Technique[技术] by (71.8m points)

php - Importer issue Invalid argument supplied for foreach()

I've a CSV files importer so the user can update the categories of the articles for the store they work for.

While the file is being processed, Laravel throws me this error:

Invalid argument supplied for foreach()

This is my Controller code:

public function procesar(ImportarCategoriasArticulos $importador){
    list( $ruta,$nombre_excel ) = explode('/', $importador->excel_ruta);
    ini_set('memory_limit', -1);
    ini_set('max_execution_time', -1);
    //dd($importador);
    try{
        CategoriaArticulos::query()->truncate();
        Excel::import(new CategoriasArticulosImportador, public_path().'/storage/uploads/'.$nombre_excel);
        $articulos_no_encontrados = CategoriaArticulos::select(['codigo_interno'])->distinct()->whereNull('articulo_id')->get();
        if( $articulos_no_encontrados->count() > 0 ){
            $importador->articulos_no_encontrados = $articulos_no_encontrados->toJson();
            $importador->estatus = 'procesado con error';
            $importador->save();
            Alert::error(trans('ERROR: Existen articulos no encontrados.'))->flash();
            return redirect('admin/configuracion/importar_categoria_articulos/'.$importador->id);
        } else {
            Articulo::all()->map( function( $articulo ){
                $nuevos_datos = CategoriaArticulos::where('articulo_id', $articulo->id)->first();
                $articulo->categoria = $nuevos_datos->categoria;
                $articulo->subcategoria = $nuevos_datos->subcategoria;
                $articulo->subsubcategoria = $nuevos_datos->subsubcategoria;
                $articulo->save();
            });

            Artisan::command('key-a:sm:generar-categorias');
            $importador->estatus = 'procesado con exito';
            $importador->save();
            Alert::success(trans('EXITO: se modificaron las categorias.'))->flash();
            return redirect('admin/configuracion/importar_categoria_articulos/');
        }
    } catch (Exception $e){
        Alert::error(trans($e->getMessage()))->flash();
        return redirect('admin/configuracion/importar_categoria_articulos/');
    }
}

Could you please help me find what is wrong? I've done importers before but this time seems impossible to me.

Thanks in advance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...