ellgreen/laravel-loadfile
Composer 安装命令:
composer require ellgreen/laravel-loadfile
包简介
A package to help with loading files into MySQL tables
README 文档
README
A package to help with loading files into MySQL tables.
This uses MySQL's LOAD DATA statement to load text files quickly into your database.
This is usually 20 times faster than using INSERT statements according to: https://dev.mysql.com/doc/refman/8.0/en/insert-optimization.html
Options
This library currently can handle any of the options in a normal LOAD DATA statement except for the partitioned table support. This will be included in a future release of laravel-loadfile.
Further information on the following options that can be passed to the load file builder can be found here:
https://dev.mysql.com/doc/refman/8.0/en/load-data.html
Installation
Requires >= PHP 8.2 and >= Laravel 12
Older versions of Laravel and PHP are supported through previous major versions of this library
composer require ellgreen/laravel-loadfile
Loading files
To use local files you will need to have local_infile enabled for
the client and server. To do this on the Laravel side you will need
to add the following to the options part of your database config:
'config' => [ PDO::MYSQL_ATTR_LOCAL_INFILE => true, ],
Simple file import
use EllGreen\LaravelLoadFile\Laravel\Facades\LoadFile; LoadFile::file('/path/to/employees.csv', $local = true) ->into('employees') ->columns(['forename', 'surname', 'employee_id']) ->load();
Ignoring header row
LoadFile::file('/path/to/employees.csv', $local = true) ->into('employees') ->columns(['forename', 'surname', 'employee_id']) ->ignoreLines(1) ->load();
Specifying field options
LoadFile::file('/path/to/employees.csv', $local = true) ->into('employees') ->columns(['forename', 'surname', 'employee_id']) // like this ->fieldsTerminatedBy(',') ->fieldsEscapedBy('\\\\') ->fieldsEnclosedBy('"') // or ->fields(',', '\\\\', '"') ->load();
Specifying line options
LoadFile::file('/path/to/employees.csv', $local = true) ->into('employees') ->columns(['forename', 'surname', 'employee_id']) // like this ->linesStartingBy('') ->linesTerminatedBy('\\n') // or ->lines('', '\\n') ->load();
Input preprocessing (set)
LoadFile::file('/path/to/employees.csv', $local = true) ->into('employees') ->columns([ DB::raw('@forename'), DB::raw('@surname'), 'employee_id', ]) ->set([ 'name' => DB::raw("concat(@forename, ' ', @surname)"), ]) ->load();
Using a different connection
LoadFile::connection('mysql') ->file('/path/to/employees.csv', $local = true) ->into('employees') ->columns(['forename', 'surname', 'employee_id']) ->load();
Duplicate-key and error handling
LoadFile::connection('mysql') ->file('/path/to/employees.csv', $local = true) ->replace() // or ->ignore() ->into('employees') ->load();
Loading data into Eloquent Models
Simply add the LoadsFiles trait to your model like so:
use EllGreen\LaravelLoadFile\Laravel\Traits\LoadsFiles; class User extends Model { use LoadsFiles; }
Then you can use the following method to load a file into that table:
User::loadFile('/path/to/users.csv', $local = true);
Need to specify options to load the file with?
Add the following method to your Model
class User extends Model { use LoadsFiles; public function loadFileOptions(Builder $builder): void { $builder ->fieldsTerminatedBy(',') ->ignoreLines(1); } }
Or you can get an instance of the query builder on the fly
User::loadFileBuilder($file, $local) ->replace() ->ignoreLines(1) ->load();
Development
Check
Runs linting, static analysis, and unit tests.
composer check
All tests
Runs unit and feature tests against all support Laravel versions.
You will need to have docker installed for these.
composer test
ellgreen/laravel-loadfile 适用场景与选型建议
ellgreen/laravel-loadfile 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 195.1k 次下载、GitHub Stars 达 82, 最近一次更新时间为 2021 年 05 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 ellgreen/laravel-loadfile 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ellgreen/laravel-loadfile 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 195.1k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 82
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-05-03