2

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.

4
  • Your parameters to the job looks like it's in the wrong order - is it supposed to be like that? You're passing $file first, but expecting $file to be the second parameter in the constructor? Commented Mar 5, 2019 at 7:07
  • @DelenaMalan I update my code. Commented Mar 5, 2019 at 7:12
  • What queue driver are you using? Commented Mar 5, 2019 at 7:16
  • @DelenaMalan i used QUEUE_DRIVER=database. Commented Mar 5, 2019 at 7:45

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

That work very well, but according to documentation batchSize is related to ToModel concern, which is not the case for ToCollection plus WithChunkReading traits. At last, WithStartRow makes possible to start from a specific row, which is useful during development process, but in this example is not explicit necessary.
Glad It helped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.