承接 jeroenzwart/laravel-csv-seeder 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jeroenzwart/laravel-csv-seeder

Composer 安装命令:

composer require jeroenzwart/laravel-csv-seeder

包简介

Seed the database with Laravel using CSV files

README 文档

README

PHP from Packagist Latest Version on Packagist GitHub code size in bytes Total Downloads Scrutinizer code quality (GitHub/Bitbucket)

Laravel CSV Seeder

Seed your database using CSV files with Laravel or Lumen

With this package you can save time for seeding your database. Instead of typing out the seeder files, you can use CSV files to fill up the database of your project. There are configuration options available to control the insert the data of your CSV files.

Features

  • Automatically try to resolve CSV filename to table name.
  • Automatic mapping of CSV headers to table column names.
  • Skip seeding data with a prefix at in the CSV headers.
  • Parse values with custom closure.
  • Hash values with a given array of column names.
  • Seed default values in to table columns.
  • Adjust Laravel's timestamp at seeding.

Installation

  • Require this package directly by

    composer require --dev jeroenzwart/laravel-csv-seeder

  • Or add this package in your composer.json and run composer update

    "jeroenzwart/laravel-csv-seeder": "1.*"

Basic usage

Extend your seed classes with JeroenZwart\CsvSeeder\CsvSeeder and set the variable $this->file with the path of the CSV file. Tablename is not required, if the filename of the CSV is the same as the tablename. At last call parent::run() to seed. A seed class will look like this;

use JeroenZwart\CsvSeeder\CsvSeeder;

class UsersTableSeeder extends CsvSeeder
{
    public function __construct()
    {
        $this->file = '/database/seeds/csvs/users.csv';
    }

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // Recommended when importing larger CSVs
        DB::disableQueryLog();
        parent::run();
    }
}

Place your CSV into the path /database/seeds/csvs/ of your Laravel project or whatever path you specify in the constructor. As default the given CSV require a header row with names, matching the columns names of the table in your database. Looks like this;

first_name,last_name,birthday
Foo,Bar,1970-01-01
John,Doe,1980-01-01

Configuration

  • tablename (string NULL) - Name of table to insert data.
  • connection (string NULL) - Name of database connection.
  • truncate (boolean TRUE) - Truncate the table before seeding.
  • foreignKeyCheck (boolean FALSE) - Enable or disable the foreign key checks.
  • header (boolean TRUE) - CSV has a header row, set FALSE if not.
  • mapping (array []) - Associative array of column names in order as CSV, if empty the first row of CSV will be used as header.
  • aliases (array []) - Associative array of CSV header names and column names; csvColumnName => aliasColumnName.
  • skipper (string %) - Skip a CSV header and data to import in the table.
  • validate (array []) - Validate a CSV row with Laravel Validation.
  • parsers (array []) - Associative array of column names to parse a value with the given closure.
  • hashable (array ['password']) - Array of column names to hash their values. It uses Hash::make().
  • empty (boolean FALSE) - Set TRUE for keeping an empty value in the CSV file to an empty string instead of a NULL.
  • defaults (array []) - Array of table columns and it's values to seed, when they are empty in the CSV file.
  • timestamps (string|boolean TRUE) - Set Laravel's timestamp in the database while seeding; set as TRUE will use current time.
  • delimiter (string ;) - The used delimiter in the CSV files.
  • chunk (integer 50) - Insert the data of rows every chunk while reading the CSV.
  • encode (boolean TRUE) - Encode the value of rows to UTF-8

Tip

Users of Microsoft Excel can use a macro to export there worksheets to CSV. Easiest is to name your worksheets as table name. Use the following macro to export;

Public Sub SaveWorksheetsAsCsv()
ActiveWorkbook.Save
Dim xWs As Worksheet
Dim xDir As String
Dim folder As FileDialog
Set folder = Application.FileDialog(msoFileDialogFolderPicker)
If folder.Show <> -1 Then Exit Sub
xDir = folder.SelectedItems(1)
For Each xWs In Application.ActiveWorkbook.Worksheets
xWs.SaveAs xDir & "\" & xWs.Name, xlCSV
Next
End Sub

Examples

Table with given timestamps

Give the seeder a specific table name instead of using the CSV filename;

    public function __construct()
    {
        $this->file = '/database/seeds/csvs/users.csv';
        $this->tablename = 'email_users';
        $this->timestamps = '1970-01-01 00:00:00';
    }

Connection

Give the seeder a specific connection and table name.;

    public function __construct()
    {
        $this->file = '/database/seeds/csvs/other_users.csv';
        $this->connection = 'second_db';
        $this->tablename = 'second_users';
    }

Mapping

Map the CSV headers to table columns, with the following CSV;

1,Foo,Bar
2,John,Doe

Handle like this;

    public function __construct()
    {
        $this->file = '/database/seeds/csvs/users.csv';
        $this->mapping = ['id', 'firstname', 'lastname'];
        $this->header = FALSE;
    }

Aliases with defaults

Seed a table with aliases and default values, like this;

    public function __construct()
    {
        $this->file = '/database/seeds/csvs/users.csv';
        $this->aliases = ['csvColumnName' => 'table_column_name', 'foo' => 'bar'];
        $this->defaults = ['created_by' => 'seeder', 'updated_by' => 'seeder'];
    }

Skipper

Skip a column in a CSV with a prefix. For example you use id in your CSV and only usable in your CSV editor. The following CSV file looks like so;

%id,first_name,last_name,%id_copy,birthday
1,Foo,Bar,1,1970-01-01
2,John,Doe,2,1980-01-01

The first and fourth value of each row will be skipped with seeding. The default prefix is '%' and changeable to;

    public function __construct()
    {
        $this->file = '/database/seeds/csvs/users.csv';
        $this->skipper = 'custom_';
    }

Validate

Validate each row of a CSV like this;

    public function __construct()
    {
        $this->file = '/database/seeds/csvs/users.csv';
        $this->validate = [ 'name'              => 'required',
                            'email'             => 'email',
                            'email_verified_at' => 'date_format:Y-m-d H:i:s',
                            'password'          => ['required', Rule::notIn([' '])]];
    }

Parse

Parse values of certain column when seeding a CSV like this;

    public function __construct()
    {
        $this->file = '/database/seeds/csvs/users.csv';
        $this->parsers = ['email' => function ($value) { 
            return strtolower($value);
        }];
    }

Hash

Hash values when seeding a CSV like this;

    public function __construct()
    {
        $this->file = '/database/seeds/csvs/users.csv';
        $this->hashable = ['password', 'salt'];
    }

License

Laravel CSV Seeder is open-sourced software licensed under the MIT license. Please see the license file for more information

Donation

If this project helped you to reduce some time to develop, you can donate me a beer :)
By Bitcoin 17jnh8oBkgLpXo3d9Xmq6i6hhYgooaYiGf or the link below;

paypal

jeroenzwart/laravel-csv-seeder 适用场景与选型建议

jeroenzwart/laravel-csv-seeder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 411.5k 次下载、GitHub Stars 达 94, 最近一次更新时间为 2018 年 08 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「excel」 「csv」 「laravel」 「seeds」 「seeding」 「seed」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 jeroenzwart/laravel-csv-seeder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 jeroenzwart/laravel-csv-seeder 我们能提供哪些服务?
定制开发 / 二次开发

基于 jeroenzwart/laravel-csv-seeder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 411.5k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 97
  • 点击次数: 31
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 94
  • Watchers: 2
  • Forks: 32
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-08-07