php - 如何在 Laravel 8 迁移期间将数据插入表中?
问题描述
我正在使用 Laravel 8 并使用 MYSQL 作为数据库航行,我必须创建一个国家表,其中所有国家的名称为“名称”列,世界上有 194 个国家,我不想插入每个国家的名字一个一个,有什么简单的方法吗?
解决方案
先创建迁移
php artisan make:migration create_countries_table
在create_countries_table
public function up()
{
Schema::create('countries', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}
和
php artisan make:seeder CountriesTableSeeder
而在 国家/地区TableSeeder
public function run()
{
DB::table('countries')->insert(
['name' => 'Afghanistan'],
['name' => 'Albania']
....
);
}
您也可以使用模型创建播种机
Country::create(
有用的链接
推荐阅读
- php - 从 Stancl/Laravel Tenancy 的租户域内的中心域获取意外数据
- bash - 如何将最终结果传输到 bash 文件中?
- python - 在水下滚动会导致:TypeError: cannot convert the series to
- primeng - PrimeNG 芯片溢出行为
- python - 如何在数字海洋上上传我的 odoo 自定义模块?
- java - 如何在清单中制作一个带有第三方依赖 jar 的 jar(没有任何 ide)
- macos - 从最近的应用程序中删除 Dock 中的图标
- python - Python:将列表转换为变量中的逗号分隔值以传递 where IN 条件
- c# - 检查用户名是否在数据库中可用
- jquery - Firefox 浏览器上的 Dialogflow Messenger 高度自定义问题