迁移后的 Laravel 种子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28463574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:54:10  来源:igfitidea点击:

Laravel seed after migrating

phplaravellaravel-4migrationlaravel-5

提问by imperium2335

Is there something I can put in my migrations to automatically seed the table with test data once the migration has completed?

一旦迁移完成,我是否可以在迁移中放入一些东西来自动为表设置测试数据?

Or do you haveto seed separately?

还是必须分开播种?

回答by lukasgeiter

You can call migrate:refreshwith the --seedoption to automatically seed after the migrations are complete:

您可以致电migrate:refresh--seed选项可自动种子后迁移完成:

php artisan migrate:refresh --seed

This will rollback and re-run all your migrations and run all seeders afterwards.

这将回滚并重新运行您的所有迁移,然后运行所有播种机。



Just as a little extra, you can also always use Artisan::call()to run an artisan command from inside the application:

作为额外的一点,您还可以始终使用Artisan::call()从应用程序内部运行工匠命令:

Artisan::call('db:seed');

or

或者

Artisan::call('db:seed', array('--class' => 'YourSeederClass'));

if you want specific seeder class.

如果你想要特定的播种机类。

回答by Manpreet

If you don't want to delete existing data and seed after migrating

如果您不想在迁移后删除现有数据和种子

lukasgeiter's answeris correct for test data, but running following artisan command

lukasgeiter 的答案对于测试数据是正确的,但是按照 artisan 命令运行

php artisan migrate:refresh --seed

in production will refresh your database removing any data entered or updated from frontend.

在生产中将刷新您的数据库,删除从前端输入或更新的任何数据。

If you want to seed your database along a migration (example rolling out an update to your application keeping existing data), like adding a new table countries along with seed data, you can do the following:

如果您想在迁移过程中为数据库设置种子(例如,对保留现有数据的应用程序进行更新),例如添加新表国家和种子数据,您可以执行以下操作:

Create a database seeder example YourSeeder.php and your location table migration

创建数据库播种器示例 YourSeeder.php 和您的位置表迁移

class YourTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tablename', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name',1000);
            $table->timestamps();
            $table->softDeletes();
        });

        $seeder = new YourTableSeeder();
        $seeder->run();
    }

    /**
    * Reverse the migrations.
    *
    * @return void
    */
    public function down()
    {
        Schema::dropIfExists('tablename');
    }
}

回答by Jason McCreary

While lukasgeiter's answeris correct, I'd like to elaborate on your second question.

虽然lukasgeiter 的回答是正确的,但我想详细说明您的第二个问题。

Or do you have to seed separately?

还是必须分开播种?

Yes. Since you're talking about test datayou should avoid coupling seedingwith migration. Of course if this were not test data, but application data, you could always make inserting data part of the migration.

是的。既然你在谈论测试数据,你应该避免将播种迁移结合起来。当然,如果这不是测试数据,而是应用程序数据,您始终可以将插入数据作为迁移的一部分。

As an aside, if you want to seed your data as part of testing, you can call $this->seed()from within your Laravel test case.

顺便说一句,如果你想将你的数据作为测试的一部分,你可以$this->seed()从你的 Laravel 测试用例中调用。