pmatseykanets/artisan-io

Laravel的Artisan数据导入命令

v3.0.0 2020-11-01 00:22 UTC

This package is auto-updated.

Last update: 2024-09-14 00:59:31 UTC


README

StyleCI tests Latest Stable Version License

此包为您的Laravel项目添加数据导入功能。它包含一个Artisan命令import:delimited,正如其名,允许您将分隔数据(CSV、TSV等)导入到本地或远程数据库。

主要功能

  • 支持多个数据库连接(在config\database.php中定义)。
  • 您可以使用表名或Eloquent模型类导入数据。使用Eloquent模型可以受益于转换器和访问器
  • 导入模式
    • 插入
    • 插入新记录
    • 更新
    • 更新并插入
  • 行验证规则

如果您觉得这个包很有用,请考虑请我喝杯咖啡。

Buy Me a Coffee at ko-fi.com

安装

您可以通过composer安装此包

composer require pmatseykanets/artisan-io

如果您正在使用Laravel < 5.5或如果已关闭包自动发现,则必须手动注册服务提供程序

// config/app.php
'providers' => [
    ...
    ArtisanIo\ArtisanIoServiceProvider::class,
],

或者您也可以自己注册命令

在您选择的编辑器中打开app\Console\Kernel.php,并将命令添加到$commands数组中

protected $commands = [
    \ArtisanIo\Console\ImportDelimitedCommand::class,
];

用法

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

示例

假设我们有一个employee.csv文件

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

employee,其迁移可能如下所示

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();
});

模型\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'
    ];
}

插入

如果employees表为空且您想填充它

php artisan import:delimited employee.csv "\App\Employee" -f email:0,firstname:1,lastname:2,phone:4,employed_on:3 -m insert

注意:在这种情况下使用Eloquent模型的好处是,时间戳created_atupdated_at将由Eloquent自动填充。

更新并插入

现在假设John的记录已存在于表中。为了更新Jon的记录并插入Jane的记录,您需要更改模式并指定键字段。

php artisan import:delimited employee.csv "\App\Employee" -f email:0,firstname:1,lastname:2,phone:4,employed_on:3 -m upsert -k email

更新

如果您只想更新现有记录的联系电话号码

php artisan import:delimited employee.csv "\App\Employee" -f email:0,phone:4 -m update -k email

字段定义文件

每个字段定义都单独占一行,格式如下

<fieldname>[:position]

其中position是字段在数据文件中的序号位置。位置是从0开始的,可以省略。

示例employee.fld

email:0
firtname:1
lastname:2
phone:4
employed_on:3

行验证规则文件

行验证规则文件是一个简单的PHP文件,它返回一个规则数组。您可以添加任何可用的Laravel验证规则

示例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'));
];

许可证

artisan-io是开源软件,许可协议为MIT许可证