Import large excel file with laravel

Tạo project với tên: import_excel

composer create-project --prefer-dist laravel/laravel import_excel "5.4.*"

Tạo database cho job

php artisan queue:table
php artisan queue:failed-table
php artisan migrate

Tạo table city

CREATE TABLE `city` (
	`id` INT(11) NOT NULL AUTO_INCREMENT,
	`name` VARCHAR(250) NULL DEFAULT NULL COLLATE 'utf8_general_ci',
	`created_date` DATETIME NULL DEFAULT NULL,
	PRIMARY KEY (`id`) USING BTREE
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB

Cấu hình file .env

QUEUE_DRIVER=database

File data excel ở folder public : vietnam.xlsx

Setup laravel excel:

composer require "maatwebsite/excel:~2.1.0"

Edit config/app.php thêm vào providers

Maatwebsite\Excel\ExcelServiceProvider::class,

Edit config/app.php thêm vào aliases

'Excel' => Maatwebsite\Excel\Facades\Excel::class,

Tạo và thêm Job vào queue

php artisan make:job ImportExcel

Tạo route import

Route::get('/import','ImportController@import');

Tạo controller ImportController

Import job :
http://localhost/import_excel/public/index.php/import

Run queue

php artisan queue:work --timeout=0

Source code here

0 Shares

Leave a Reply

avatar
  Subscribe  
Notify of