首页 > 解决方案 > 为现有用户添加数据 - Laravel

问题描述

我正在我的生产应用程序中添加一个名为user_settings. 对于所有未来的用户注册时,它将创建一个新记录。为现有用户“播种”数据的最佳方式是什么?

Laravel 文档说播种机用于测试假数据。

那么基本上有如何做到这一点的行业标准吗?因为我所有现有的用户都需要拥有user_settings,否则该应用程序将不适合他们。

到目前为止的想法是:

标签: laravelmigration

解决方案


您可以做的是编写迁移以将数据插入表中,

您将不得不调整创建的默认迁移文件,这样您就可以在生产服务器上运行该迁移以及为现有用户创建数据。

这是一个例子。

public function up()
{
// Create the table
Schema::create('users', function($table){
    $table->increments('id');
    $table->string('email', 255);
    $table->string('password', 64);
    $table->boolean('verified');
    $table->string('token', 255);
    $table->timestamps();
});

// Insert some stuff
DB::table('users')->insert(
    array(
        'email' => 'name@domain.com',
        'verified' => true
    )
);

}


推荐阅读