欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

laravel批量数据填充

程序员文章站 2022-05-18 21:06:41
...

1.查看数据库
laravel批量数据填充
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

效果图
laravel批量数据填充

相关标签: 笔记