Laravel数据填充

2017-11-23  本文已影响0人  老王谈编程

框架:Laravel
版本:5.5.21

$ php artisan make:factory StudentsFactory
$ php artisan make:seeder StudentsTableSeeder
$factory->define(Student::class, function (Faker $faker) {
    $time = date('Y-m-d H:i:s');
    return [
        'name' => $faker->unique()->name,
        'age' => rand(1,120),
        'sex' => rand(0,2),
        'created_at' => $time,
        'updated_at' => $time,
    ];
});
public function run()
{
    $students = factory(\App\Models\Student::class)->times(300)->make();
    \App\Models\Student::insert($students->toArray());
}
public function run()
{
    $this->call(StudentsTableSeeder::class);
}
$ php artisan db:seed --class=StudentsTableSeeder
上一篇下一篇

猜你喜欢

热点阅读