pmatseykanets/artisan-io
Composer 安装命令:
composer require pmatseykanets/artisan-io
包简介
Artisan data import command for Laravel
README 文档
README
This package adds data import capability to your Laravel project. It contains an artisan command import:delimited which allows you, as the name implies, to import delimited data (CSV, TSV, etc) into your local or remote database.
Main features:
- Supports multiple database connections (defined in
config\database.php). - You can use either a table name or Eloquent model class to import your data. By using Eloquent model you can benefit from mutators and accessors.
- Import modes:
- insert
- insert-new
- update
- upsert
- Row validation rules
If you find this package usefull, please consider bying me a coffee.
Installation
You can install the package via composer:
composer require pmatseykanets/artisan-io
If you're using Laravel < 5.5 or if you have package auto-discovery turned off you have to manually register the service provider:
// config/app.php 'providers' => [ ... ArtisanIo\ArtisanIoServiceProvider::class, ],
Alternatively you can register the command yourself
Open app\Console\Kernel.php in the editor of your choice and add the command to the $commands array
protected $commands = [ \ArtisanIo\Console\ImportDelimitedCommand::class, ];
Usage
php artisan import:delimited --help Usage: import:delimited [options] [--] <from> <to> Arguments: from The path to an import file i.e. storage/import.csv to The table or Eloquent model class name Options: -f, --fields[=FIELDS] A comma separated list of field definitions in a form <field>[:position] i.e. "email:0,name,2". Positions are 0 based -F, --field-file[=FIELD-FILE] Path to a file that contains field definitions. One definition per line -m, --mode[=MODE] Import mode [insert|insert-new|update|upsert] [default: "upsert"] -k, --key[=KEY] Field names separated by a comma that constitute a key for update, upsert and insert-new modes -R, --rule-file[=RULE-FILE] Path to a file that contains field validation rules -d, --delimiter[=DELIMITER] Field delimiter [default: ","] -i, --ignore[=IGNORE] Ignore first N lines of the file -t, --take[=TAKE] Take only M lines -c, --database[=DATABASE] The database connection to use -x, --transaction Use a transaction --dry-run Dry run mode --no-progress Don't show the progress bar --force Force the operation to run when in production
Examples
Lets say we have employee.csv file
email,firstname,lastname,employed_on,phone
john.doe@example.com,John,Doe,07/01/2014,2223334455
jane.doe@example.com,Jane,Doe,02/15/2015,5554443322
table employee the migration for which may look like
Schema::create('employees', function (Blueprint $table) { $table->increments('id'); $table->string('email')->unique(); $table->string('firstname', 60)->nullable(); $table->string('lastname', 60)->nullable(); $table->string('phone', 10)->nullable(); $table->date('employed_on')->nullable(); $table->timestamps(); });
and model \App\Employee
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Employee extends Model { protected $table = 'employees'; protected $fillable = [ 'email', 'firstname', 'lastname', 'phone', 'employed_on' ]; }
Insert
If employees table is empty and you'd like to populate it
php artisan import:delimited employee.csv "\App\Employee" -f email:0,firstname:1,lastname:2,phone:4,employed_on:3 -m insert
Note: The buity of using Eloquent model in this case is that timestamps created_at and updated_at will be populated by Eloquent automatically.
Upsert
Now let's assume John's record is already present in the table. In order to update Jon's record and insert Jane's one you'd need to cnahge the mode and specify key field(s).
php artisan import:delimited employee.csv "\App\Employee" -f email:0,firstname:1,lastname:2,phone:4,employed_on:3 -m upsert -k email
Update
If you want to just update phone numbers for existing records
php artisan import:delimited employee.csv "\App\Employee" -f email:0,phone:4 -m update -k email
Field definition file
Each field definition goes on a separate line in the format
<fieldname>[:position]
where position is an ordinal position of the field in the data file. The position is 0-based and can be omitted.
Example employee.fld
email:0
firtname:1
lastname:2
phone:4
employed_on:3
Row validation rules file
A row validation rule file is simply a php file that returns an array of rules. You can any of the available Laravel validation rules
Example employee.rule
<?php return [ 'email' => 'required|email', 'firstname' => 'string|min:2|max:60', 'lastname' => 'string|min:2|max:60', 'phone' => 'digits:10|regex:/[2-9][0-9]{2}[2-9][0-9]{6}/' 'employed_on' => 'date_format:m/d/Y|after:2010-07-15|before:'.date('Y-m-d', strtotime('tomorrow')); ];
License
The artisan-io is open-sourced software licensed under the MIT license
pmatseykanets/artisan-io 适用场景与选型建议
pmatseykanets/artisan-io 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.97k 次下载、GitHub Stars 达 17, 最近一次更新时间为 2015 年 07 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「data」 「csv」 「import」 「laravel」 「tsv」 「delimited」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 pmatseykanets/artisan-io 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pmatseykanets/artisan-io 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 pmatseykanets/artisan-io 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
Adds the EDTF data type to Wikibase
A simple library that allows transform any kind of data to native php data or whatever
Parse use statements for a reflection object
Tool for copying data from a production database to a dev database. Also useful for making backups of production databases.
A fork of konnco/filament-import with support of Laravel 11 since the default importer of Filament 3 is nonsense for basic use case.
统计信息
- 总下载量: 1.97k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 19
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-07-21