Php 迁移后的落叶松种子

Php 迁移后的落叶松种子,php,laravel,laravel-4,migration,laravel-5,Php,Laravel,Laravel 4,Migration,Laravel 5,迁移完成后,我是否可以在迁移中添加一些东西,以便在表中自动添加测试数据种子 或者您必须单独进行种子设定吗?您可以使用--seed选项调用migrate:refresh,在迁移完成后自动进行种子设定: php artisan migrate:refresh --seed 这将回滚并重新运行所有迁移,然后运行所有播种机 另外,您还可以始终使用Artisan::call()从应用程序内部运行Artisan命令: Artisan::call('db:seed'); 或 如果您想要特定的播种机类别。

迁移完成后,我是否可以在迁移中添加一些东西,以便在表中自动添加测试数据种子


或者您必须单独进行种子设定吗?

您可以使用
--seed
选项调用
migrate:refresh
,在迁移完成后自动进行种子设定:

php artisan migrate:refresh --seed
这将回滚并重新运行所有迁移,然后运行所有播种机


另外,您还可以始终使用
Artisan::call()
从应用程序内部运行Artisan命令:

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

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

虽然正确,但我想详细说明您的第二个问题

或者你必须分开播种吗

对。因为您讨论的是测试数据,所以应该避免将种子设定与迁移耦合在一起。当然,如果这不是测试数据,而是应用程序数据,您可以将插入数据作为迁移的一部分

另一方面,如果您想将数据作为的一部分进行种子设定,您可以在Laravel测试用例中调用
$this->seed()

如果您不想删除现有数据,并且想在迁移后进行种子设定 对于测试数据是正确的,但运行以下artisan命令

php artisan migrate:refresh --seed
在生产中,将刷新您的数据库,删除从前端输入或更新的任何数据

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

在database/seeds和位置表迁移中创建一个数据库种子器示例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');
    }
}

如果您的TableSeeder类出现php类not found错误,请运行
composer dump autoload

我按照您所说的做了,在投入生产之前一切正常。生产与此冻结。有关详细信息,请参阅my。请注意,如果在DB中有种子数据以外的数据后在生产环境中运行此操作,则会丢失该数据。这应该是imo接受的答案。从迁移中看,这确实是可读的。此外,如果从迁移中调用命令,还需要添加--force标志,以防止系统在生产服务器中请求确认。这就是我要寻找的,在不更改数据库的情况下,使用默认内容创建新表。
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');
    }
}