Home » Php » php – I get a 504 timeout error when importing excel to a db (Laravel)

php – I get a 504 timeout error when importing excel to a db (Laravel)

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m trying to import 10,000 records in excel with the library “Maatwebsite / Laravel-Excel” in Laravel, but when importing 10,000 records, the page returns a 504 timeout error (the odd thing is that it keeps inserting data in the db after that). Well then, I show you my code to see if you can help me, thanks.

This is the Import Inventory, where I programmed everything I should do with the data to be imported into the db.

class InventarioImport implements ToCollection, WithChunkReading
{
    use Importable;
    /**
     * @param array $row
     *
     * @return User|null
     */

    public function chunkSize(): int
    {
        return 250;
    }


    public function collection(collection $rows)
    {
         foreach ($rows as $row)
        {

        $almacenes = Almacen::where('tienda_id', \Session::get('tienda_id'))->get();
        $codigos = Codigo::where('tienda_id', \Session::get('tienda_id'))->get();
        $contc=-1;
        $conta=0;

        foreach ($codigos as $codigo) {
            $contc++;
        }
        foreach ($almacenes as $almacen) {
            $conta++;
        }
        if($row[$contc+1]=="DESCRIPCION") {
                continue 1;

            }

        $idmarca = Marca::where('nombre_marca', $row[$contc+2])->where('tienda_id', \Session::get('tienda_id'))->first();

        if ($idmarca==null) {
           $marcaid= Marca::create([
           'nombre_marca'     => $row[$contc+2],
           'tienda_id'     => \Session::get('tienda_id'),
           'estado'     => "A",

        ])->id;
}
else {
  $marcaid = $idmarca->id;
}
$buscarcategoria = Categoria::where('nombre_categoria', $row[$contc+3])->where('tienda_id', \Session::get('tienda_id'))->first();
        if ($buscarcategoria==null) {
           Categoria::create([
           'nombre_categoria'     => $row[$contc+3],
           'tienda_id'     => \Session::get('tienda_id'),
           'estado'     => "A",

        ]);
}
$i=0;
       $buscaritem = Item::where('nombre_item', $row[$contc+1])->where('tienda_id', \Session::get('tienda_id'))->first();

 $buscarunidad = Unidad::where('nombre_unidad', $row[$contc+4])->first();


   $buscarmoneda = ConfiguracionMoneda::where('abreviacion_moneda', $row[$conta+3+$contc+4])->where('tienda_id', \Session::get('tienda_id'))->first();
    $itemid= Item::create([
            'marca_id' => $marcaid,
           'nombre_item'     => $row[$contc+1],
           'unidad_id'     => $buscarunidad->id,
           'stock_minimo'     => $row[$conta+1+$contc+4],
           'stock_maximo'     => $row[$conta+2+$contc+4],
           'moneda_id'     => $buscarmoneda->id,
           'precio'     => $row[$conta+4+$contc+4],
           'impuesto'     => $row[$conta+5+$contc+4],
           'margen_final'     => $row[$conta+6+$contc+4],
           'margen_inicio'     => $row[$conta+7+$contc+4],
           'notas'     => $row[$conta+8+$contc+4],
           'estado'     => "A",
           'tienda_id'     => \Session::get('tienda_id'),

        ])->id;
        $a=$contc+4;
        $j=$a+1;
        foreach ($almacenes as $almacen) {

       $buscaralmacen = Almacen::where('nombre_almacen', $almacen->nombre_almacen)->where('tienda_id', \Session::get('tienda_id'))->first();

          ItemStock::create([
           'item_id'     => $itemid,
           'almacen_id' => $buscaralmacen->id,
           'detalle' => "Saldo Inicial",
            'cantidad' => $row[$j],
            'tipo' => "M",

           'tienda_id'     => \Session::get('tienda_id'),
           'estado'     => "A",
        ]);


        $kardex = new Kardex();
                $kardex->item_id = $itemid;
                $kardex->fecha = date("Y/m/d");
                $kardex->operacion = "Inicial";
 $kardex->tipo = "";
 $kardex->serie = "";
 $kardex->numero = "";

                $kardex->almacen_id = $buscaralmacen->id;
                $kardex->tienda_id = \Session::get('tienda_id');
                $kardex->saldocantidad = $row[$j];
                $kardex->saldocosto = $row[$conta+4+$contc+4];
                $kardex->saldototal = $row[$conta+4+$contc+4]*$row[$j];

                $kardex->save();
                $j++;
 }

  foreach ($codigos as $codigo) {

       $buscarcodigo = Codigo::where('nombre_codigo', $codigo->nombre_codigo)->where('tienda_id', \Session::get('tienda_id'))->first();
       $buscaritem = Item::where('nombre_item', $row[$contc+1])->where('tienda_id', \Session::get('tienda_id'))->first();

          ItemCodigo::create([
           'item_codigo'     => $row[$i],
           'codigo_id' => $buscarcodigo->id,
           'item_id' => $buscaritem->id,
           'tienda_id'     => \Session::get('tienda_id'),
           'estado'     => "A",
        ]);
        $i++;


 }


    }
    }
}

This is my function in Controller, which I do the import as indicated in the previous script.

public function subirinventario(Request $request)
    {
        $title = 'Inventario subido';

        $contarr= $request->contarr;
  $arrayitems = Excel::import(new InventarioImport, $request->path);
        return view('item.finalizar', compact('contarr','title'));
    }

That would be all, I missed the routes and the views, but it works fine with few records, I have to emphasize that I have increased the runtime of nginx, restarted and nothing.

This is an image with the preview of the data and columns, only 10 are shown, but more than 10 thousand were detected.

How to&Answers:

You can try to put this 2 lines in the start of your method/function:

    ini_set('memory_limit','1024M');
    set_time_limit(3000000); //You can use 0 to remove limits

As I stated above you can use 0 inside the set_time_limit method, but i don’t recommend this, because losing all the control of your execution is not worth in most cases

Hope it works!