laravel 模拟数据 seed

发表于

1. 生成model和migration

php artisan make:model Model/Lesson -m

2.建表结构和模型

    public function up()
    {
        Schema::create(‘lessons’, function (Blueprint $table) {
            $table->increments(‘id’);
            $table->string(‘title’);
            $table->text(‘body’);
            $table->boolean(‘free’);
            $table->timestamps();
        });
    }

php artisan migrate

3.在 database\factories\CommonFactory.php中书写

// Lesson
$factory->define(App\Model\Lesson::class, function (Faker $faker) {
    return [
        ‘title’ => $faker->sentence,
        ‘body’ => $faker->paragraph,
        ‘free’ => $faker->boolean()
    ];
});

4.新建 database\seeds\LessonTableSeeder.php

run方法中书写

factory(‘App\Model\Lesson’, 50)->create();

php artisan make:seeder LessonTableSeeder

5.执行

php artisan db:seed –class=LessonTableSeeder

6.同5选其1

在 database\seeds\DatabaseSeeder.php 中 run方法下新增

$this->call(LessonTableSeeder::class);

执行

php artisan db:seed

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注