Laravel 迁移插入表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39737209/
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
Laravel Migrations insert into table
提问by Jess McKenzie
In Laravel 5.3 within my up()
function how can I insert data into another table?
在我的up()
函数中的Laravel 5.3 中,如何将数据插入到另一个表中?
I can only see things in the guide for updating columns etc
我只能在更新列等的指南中看到内容
回答by yixiang
You can do it as what you do in normal Laravel code as long as that table has been created already. For example:
只要已经创建了该表,您就可以像在普通 Laravel 代码中所做的那样进行操作。例如:
public function up()
{
Schema::create('this_table', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
\DB::table('that_table')->insert([]);
}
As others suggest, you should consider if it's better to move such logic into database seeders. However, I find sometimes it's better to have such logic just live inside migration files when your table have constant initial data.
正如其他人建议的那样,您应该考虑将此类逻辑移至数据库播种机中是否更好。但是,我发现有时当您的表具有恒定的初始数据时,将此类逻辑仅存在于迁移文件中会更好。
FYI, Hereis an open source Laravel project does this.
仅供参考,这是一个开源 Laravel 项目。