touhidurabir/laravel-seed-extender

扩展 Laravel 种子类的一种有观点的方法。

1.2.0 2024-08-17 07:02 UTC

This package is auto-updated.

Last update: 2024-09-17 07:08:57 UTC


README

一种高度有观点的方式来与 Laravel Seeder 一起工作。

安装

使用 composer 安装此包

composer require touhidurabir/laravel-seed-extender

为什么?

如前所述,这是一种高度有观点的方式来与 Seeder 一起工作。我们拥有流行的 Faker 库和 Model Factories 来填充模型表,这似乎是显而易见的选择。但有时在生产环境中,甚至是在开发过程中,我们需要真实数据来填充模型表,为此需要 Seeder 类。

现在这个包并没有引入任何新的 Seeder 类的填充机制,而是仅仅添加了一些按需操作 Seeder 类的能力。基本上,它为 Seeder 类添加了一些额外的功能/能力。

使用方法

要生成此包的新 Seeder 类,运行以下命令

php artisan make:extend-seeder SeederClassName --table=table_name

这将生成一个位于 /database/seeders 位置的新 Seeder 类。例如,一个基本的用户 Seeder 类看起来像这样

<?php

namespace Database\Seeders;

use Touhidurabir\SeedExtender\BaseTableSeeder;

class UsersTableSeeder extends BaseTableSeeder {
    
    /**
     * Seeder table name 
     *
     * @var string
     */
    protected $table = "users";


    /**
     * The table attributes/columns that will be ignored during the seeding process
     *
     * @var array
     */
    protected $ignorables = [];


    /**
     * The table attributes/columns that will be used during the seeding process
     *
     * @var array
     */
    protected $useables = [];


    /**
     * Should merge and include timestamps[created_at, updated_at] by default into the seed data
     *
     * @var boolean
     */    
    protected $includeTimestampsOnSeeding = true;


    /**
     * If define, the seeding process will utilize the eloquent model
     *
     * @var string
     */    
    protected $model = null;


    /**
     * Determine if the seeding process should run quietly without firing any model event if seed vai model
     *
     * @var boolean
     */    
    protected $quietly = true;


    /**
     * The seeding data
     *
     * @var array
     */
    protected $data = [
    	
    ];


    /**
     * Build up the seedeable data set;
     *
     * @return array
     */
    protected function seedableDataBuilder() {

        foreach ($this->data as $key => $value) {
            
            $this->data[$key] = array_merge($value, [

            ]);
        }

        return $this->data;
    }
}

如上述示例所示,有一些属性可以手动修改或通过命令修改。

类属性和方法说明

$table (属性)

它定义了将为此表运行 Seeder。可以通过命令指定哪个表

php artisan make:extend-seeder UsersTableSeeder --table=users

注意,如果未传递 $table 属性,它将默认设置表名为 table_name,需要根据目标填充表进行更新

$ignorables (属性)

它定义了在填充时将被忽略的列。它指定在类生成时想要忽略的列,通过命令提供逗号分隔的列名

php artisan make:extend-seeder UsersTableSeeder --table=users --ignorables=id,deleted_at

$useables (属性)

它定义了在填充时将使用的列。它指定在类生成时想要使用的列,通过命令提供逗号分隔的列名

php artisan make:extend-seeder UsersTableSeeder --table=users --useables=name,email,password

注意,如果已定义 $useables 属性且不为空,它将考虑这些值并忽略 $ignorables 属性的设置值。

$includeTimestampsOnSeeding (属性)

它定义了在填充时是否自动包含 created_atupdated_at 值。默认情况下,此设置为 true。但如果模型没有使用 timestamp 值或不想将其包含在填充过程中,则可以在生成时通过命令指定它

php artisan make:extend-seeder UsersTableSeeder --table=users --useables=name,email,password --no-timestamp

$model (属性)

它定义了关联的表模型。仅在想要通过 Eloquent 模型类运行填充过程时使用此属性。

注意,如果定义了 $model 属性,控制台填充过程将使用模型类来运行填充过程。

在命令中使用带完整命名空间的正确模型类并在命令中设置 -model 选项来指定您想要填充过程使用 Eloquent 模型类。

php artisan make:extend-seeder UsersTableSeeder --table=users --model='App\\Models\\User' --useables=name,email,password --no-timestamp

$quietly (属性)

此定义了在通过eloquent模型运行种子过程时,是否应该触发模型事件。默认设置为TRUE,因此当种子过程通过模型类运行时,不会触发任何模型事件,因为大多数情况下我们不想在种子时让观察者(如果已注册)执行操作。将其设置为FALSE以确保在通过eloquent模型进行种子时,模型事件将在种子时触发。

在命令中使用标志--with-events来指定希望在种子时触发模型事件。

php artisan make:extend-seeder UsersTableSeeder --table=users --model='App\\Models\\User' --with-events --useables=name,email,password --no-timestamp

$data(属性)

此定义了要种子的数据。例如

protected $data = [
    ['testuser1@test.com', '123456'],
    ['testuser2@test.com', '123456'],
];

seedableDataBuilder(方法)

如果我们有一些需要存在于每行种子中的数据,那么最好不在$data属性中重复这些数据,而是在此方法中定义它们。

protected function seedableDataBuilder() {

    foreach ($this->data as $key => $value) {
        
        $this->data[$key] = array_merge($value, [
            // any repetitive merge data 
            // it will merge to every row data defined in the $data proeprties
        ]);
    }

    return $this->data;
}

更多命令选项

replace

默认情况下,如果已存在同名的种子类,将会抛出异常并在控制台打印错误消息。如果需要替换,请传递标志--replace

php artisan make:extend-seeder UsersTableSeeder --table=users --replace

strict

默认情况下,此包不会尝试验证给定的信息,如使用的表名或可忽略的列,但如果需要,可以通过传递标志--strict来验证这些信息。

php artisan make:extend-seeder UsersTableSeeder --table=users --useables=email,password --strict

独立于种子类运行种子

此包提供了一种独立于种子类运行种子过程的方法。也就是说,可以在运行时从应用程序中运行种子过程。

use Touhidurabir\SeedExtender\Facades\SeedExtender;

SeedExtender::table('table_name') //table name
    ->useables([]) // useables columns as array
    ->ignorables([]) // ignorables columns as array
    ->includeTimestampsOnSeeding(true) // auto include of timestamp value as boolean
    ->seedData( // set seed data
        [ [], [], ], // the seed data itself
        [] // any auto mergeable repetitive data
    )
    ->throughModel(SomeModelClass::class) // specify if want to run seeding via eloquent model
    ->withModelEvents() // specific if want to have model events fire when running the seeding process via eloquent model
    ->run(); // run the seeding process

贡献

欢迎提交拉取请求。对于主要更改,请先打开一个问题来讨论您想要更改的内容。

请确保适当地更新测试。

许可

MIT