laravel - 为现有用户添加数据 - Laravel
问题描述
我正在我的生产应用程序中添加一个名为user_settings
. 对于所有未来的用户注册时,它将创建一个新记录。为现有用户“播种”数据的最佳方式是什么?
Laravel 文档说播种机用于测试假数据。
那么基本上有如何做到这一点的行业标准吗?因为我所有现有的用户都需要拥有user_settings
,否则该应用程序将不适合他们。
到目前为止的想法是:
在创建表后循环遍历所有现有用户的迁移文件中添加一个查询。
创建播种器并通过生产服务器上的命令行运行它。
迁移选项对
->default()
我不起作用,因为它可能会及时更改。
解决方案
您可以做的是编写迁移以将数据插入表中,
您将不得不调整创建的默认迁移文件,这样您就可以在生产服务器上运行该迁移以及为现有用户创建数据。
这是一个例子。
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
)
);
}
推荐阅读
- flutter - 一切正常,直到我升级新的颤振 sdk 版本
- sql - 如何在 postgres 中强制执行幻读?
- wireless - 无法无线配置 E22 400T30D
- javascript - 写入内容后如何获取 iframe 的高度
- c++ - 牛点云分析
- android - 带有更新的预填充数据库的 Android Room 迁移
- mysql - 将SHA1的所有情况的哈希集存储在数据库中时的搜索时间
- timescaledb - Timescaledb:如何停止数据库中的所有连续聚合作业
- xamarin - 如何在 Xamarin 表单中将我的按钮计数重置为 0?
- visual-studio-code - DropDownList cacadierend