zaw naing
zaw naing

Reputation: 21

Import large excel file using laravel excel and laravel queue

laravel excel version 3.1 laravel version 5.6 i have over 100000 rows of data in excel file. I would like to import this data into my database.

In My controller.php

if(request()->file('fertilizer_import')) {
    $import = new FertilizerImport();
    $file = request()->file('fertilizer_import');
    dispatch(new FertilizerImportJob($import, $file));
}

In my FertilizerImportJob.php

public function __construct($import, $file)
{
    $this->import = $import;
    $this->file = $file;
}
public function handle()
{
    Excel::import($this->import, $this->file);
}

And then, I uploaded my excel file. It is enter one row in jobs table. I run php artisan make:queue but data is not enter my fertilizer table. How can i do that? Please advice me.

Upvotes: 2

Views: 15705

Answers (1)

Furquan
Furquan

Reputation: 1842

You should Try to use chunk and queue in laravel/Maatwebsite In controller file

public function importExcel()
    {
        $path = resource_path() . "/Houten.xlsx";
        \Excel::import(new TestImport, $path);
        return redirect('/')->with('success', 'All good!');
    return back();
    }

And in Your Import File app/Imports/testImports.php

use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Illuminate\Contracts\Queue\ShouldQueue;
use Maatwebsite\Excel\Concerns\WithChunkReading;

class TestImport implements ToCollection, WithChunkReading, ShouldQueue,WithStartRow
{
    public function collection(Collection $rows)
    {
        //do your insertion here. //no seprate job required
    }

     public function startRow(): int 
    {
         return 1;
    }

    public function batchSize(): int
    {
        return 500;
    }

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

Then set QUEUE_DRIVER=database and run the code. You will see one entry in jobs table. then you need to run this job. there are multiple ways to run any job. below is one In your terminal

php artisan queue:restart
php artisan queue:listen

this will execute the code in collection function in chunks and will insert your data.

Upvotes: 2

Related Questions