laravel批量数据填充
程序员文章站
2022-05-18 21:06:41
...
1.查看数据库
2.创建seeder
php artisan make:seeder ArticlesTableSeeder
3.ArticlesTableSeeder
<?php
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class Articlesairport extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//产生实例
$fack = \Faker\Factory::create();
$collect = collect(['Beijing', 'Shanghai', 'Guangzhou', 'Hangzhou', 'Xiamen', 'Shenzhen']);
$data = [];
//随机插入十条数据
for ($i = 0; $i < 50; $i++) {
$date = [
'aip_num' => 'NO.' . rand(0, 9) . rand(0, 9) . rand(0, 9) . rand(0, 9) . rand(0, 9),
'kind' => rand(100, 998) . 'ER',
't_start' => date('Y-m-d H:i:s', strtotime("+" . rand(1, 50) . "hour")),
's_city' => $collect->random(),
'e_city' => $collect->random(),
'fc' => rand(0, 100),
'bc' => rand(0, 100),
'ec' => rand(0, 100)
];
// }
DB::table('airports')->insert($date);
}
}
}
4.执行db
php artisan db:seed --class=Articlesairport
效果图