devlabor/laravel-csv-seeder

1.0.0 2019-09-29 09:52 UTC

This package is auto-updated.

Last update: 2024-09-29 05:22:02 UTC


README

本包基于 "spatie/laravel-query-builder",允许您快速为Laravel应用程序创建API控制器。此包还与授权策略一起工作。

基本用法

创建一个新的表生成器类: php artisan make:seeder ProductsTableSeeder 并将内容更改为以下代码。CSV文件必须位于 /database/seeds/csvs/products.csv

use \DevLabor\CsvSeeder\Database\Seeder\CsvSeeder

// ...

class ProductsTableSeeder extends CsvSeeder {
	/**
	 * UsersTableSeeder constructor.
	 */
	public function __construct()
	{
		$this->columnMapping = [
			0 => 'article_no',
			1 => 'name',
			2 => 'text',
			3 => 'price'
		];
	}
	
	/**
	 * Run the database seeds.
	 *
	 * @return void
	 */
	public function run()
	{
		// Recommended when importing larger CSVs
		\Illuminate\Support\Facades\DB::disableQueryLog();
	
		// Uncomment the below to wipe the table clean before populating
		\Illuminate\Support\Facades\DB::table($this->guessTableName())->truncate();
	
		parent::run();
	}
}

安装

您可以通过composer安装此包

composer require devlabor/laravel-csv-seeder

用法

CsvSeeder类易于使用,通常独立工作。有时您可能需要或希望自定义CSV导入文件的参数。

参数

// Database table name for seeding. Default guessed by class name.
public $table = '';
// CSV file name for seeding. Default guessed by class name.
public $filename = '';
// Table field that to be hashed, most likely a password field. If your password has a different name, please overload this variable from our seeder class.
public $hashable = 'password';
// An SQL INSERT query will execute every time this number of rows are read from the CSV. Without this, large INSERTS will silently fail.
public $insertChunkSize = 50;
// CSV delimiter
public $csvDelimiter = ';';
// Number of rows to skip at the start of the CSV
public $offsetRows = 0;
// Can be used to tell the import to trim any leading or trailing white space from the column;
public $trimWhitespace = true;
/**
 * The mapping of CSV to table column. If not specified manually, the first row (after $offsetRows) of your CSV will be read as your table columns.
 *
 * In order to read the first, third and fourth columns of your CSV only, use:
 * array(
 *   0 => id,
 *   2 => name,
 *   3 => description,
 * )
 */
public $columnMapping = [];

测试

composer test

安全

如果您发现任何与安全相关的问题,请通过电子邮件 office@devlabor.com 通知我们,而不是使用问题跟踪器。

许可证

MIT许可证(MIT)。有关更多信息,请参阅 许可证文件