flynsarmy/csv-seeder
Composer 安装命令:
composer require flynsarmy/csv-seeder
包简介
Allows seeding of the database with CSV files
README 文档
README
Seed your database with CSV files
This package allows CSV based seeds.
Installation
Require this package in your composer.json and run composer update (or run composer require flynsarmy/csv-seeder:2.* directly):
For PHP 7.4+
"flynsarmy/csv-seeder": "2.0.*"
For older PHP versions
"flynsarmy/csv-seeder": "1.*"
Usage
Your CSV's header row should match the DB columns you wish to import. IE to import id and name columns, your CSV should look like:
id,name 1,Foo 2,Bar
Seed classes must extend Flynsarmy\CsvSeeder\CsvSeeder, they must define the destination database table and CSV file path, and finally they must call parent::run() like so:
use Flynsarmy\CsvSeeder\CsvSeeder; class StopsTableSeeder extends CsvSeeder { public function __construct() { $this->table = 'your_table'; $this->filename = base_path().'/database/seeds/csvs/your_csv.csv'; } public function run() { // Recommended when importing larger CSVs DB::disableQueryLog(); // Uncomment the below to wipe the table clean before populating DB::table($this->table)->truncate(); parent::run(); } }
Drop your CSV into /database/seeds/csvs/your_csv.csv or whatever path you specify in your constructor above.
Configuration
In addition to setting the database table and CSV filename, the following configuration options are available. They can be set in your class constructor:
connection(string '') Connection to use for inserts. Leave empty for default connection.insert_chunk_size(int 500) An SQL insert statement will trigger everyinsert_chunk_sizenumber of rows while reading the CSVcsv_delimiter(string ,) The CSV field delimiter.hashable(array [password]) List of fields to be hashed before import, useful if you are importing users and need their passwords hashed. UsesHash::make(). Note: This is EXTREMELY SLOW. If you have a lot of rows in your CSV your import will take quite a long time.offset_rows(int 0) How many rows at the start of the CSV to ignore. Warning: If used, you probably want to set a mapping as your header row in the CSV will be skipped.mapping(array []) Associative array of csvCol => dbCol. See examples section for details. If not specified, the first row (after offset) of the CSV will be used as the mapping.should_trim(bool false) Whether to trim the data in each cell of the CSV during import.timestamps(bool false) Whether or not to add created_at and updated_at columns on import.created_at(string current time in ISO 8601 format) Only used iftimestampsistrueupdated_at(string current time in ISO 8601 format) Only used iftimestampsistrue
Examples
CSV with pipe delimited values:
public function __construct() { $this->table = 'users'; $this->csv_delimiter = '|'; $this->filename = base_path().'/database/seeds/csvs/your_csv.csv'; }
Specifying which CSV columns to import:
public function __construct() { $this->table = 'users'; $this->csv_delimiter = '|'; $this->filename = base_path().'/database/seeds/csvs/your_csv.csv'; $this->mapping = [ 0 => 'first_name', 1 => 'last_name', 5 => 'age', ]; }
Trimming the whitespace from the imported data:
public function __construct() { $this->table = 'users'; $this->csv_delimiter = '|'; $this->filename = base_path().'/database/seeds/csvs/your_csv.csv'; $this->mapping = [ 0 => 'first_name', 1 => 'last_name', 5 => 'age', ]; $this->should_trim = true; }
Skipping the CSV header row (Note: A mapping is required if this is done):
public function __construct() { $this->table = 'users'; $this->csv_delimiter = '|'; $this->filename = base_path().'/database/seeds/csvs/your_csv.csv'; $this->offset_rows = 1; $this->mapping = [ 0 => 'first_name', 1 => 'last_name', 2 => 'password', ]; $this->should_trim = true; }
Specifying the DB connection to use:
public function __construct() { $this->table = 'users'; $this->connection = 'my_connection'; $this->filename = base_path().'/database/seeds/csvs/your_csv.csv'; }
Migration Guide
2.0
$seeder->hashableis now anarrayof columns rather than a single column name. Wrap your old string value in[].
License
CsvSeeder is open-sourced software licensed under the MIT license
flynsarmy/csv-seeder 适用场景与选型建议
flynsarmy/csv-seeder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.73M 次下载、GitHub Stars 达 256, 最近一次更新时间为 2014 年 02 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「csv」 「laravel」 「seeds」 「seeding」 「seed」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 flynsarmy/csv-seeder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 flynsarmy/csv-seeder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 flynsarmy/csv-seeder 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Create and use JSON files to seed your database in your Laravel applications
Only seed what you haven't seeded yet.
Test data for laravel-excel-seeder
Allows seeding of the database with CSV files and extract clear unit from string
laravel facade to read/write csv file
Collection of tools to use the full power of the Enyalius framework
统计信息
- 总下载量: 1.73M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 259
- 点击次数: 27
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-02-13