Recipe-box 数据填充

生成填充器(完成之后可在 database/migrations 下看到创建的文档)

>php artisan make:seed UsersTableSeeder
>php artisan make:seed RecipesTableSeeder

修改 DatabaseSeeder.php 的 run方法(路径:database/seeds/DatabaseSeeder.php)

 public function run()
{
    $this->call(UsersTableSeeder::class);
    $this->call(RecipesTableSeeder::class);
}

编写填充器

修改UsersTableSeeder.php

 $faker->name,
                'email' => $faker->email,
                'password' => bcrypt('password'),
                'api_token' => str_random(60)
            ]);
        }
    }
}

修改RecipesTableSeeder.php

 $i,
                'name' => $faker->sentence,
                'description' => $faker->paragraph(mt_rand(5,15)),
                'image' => 'test.png'
            ]);
            foreach (range(1, mt_rand(3,12))as $j){
                RecipeIngredient::create([
                    'recipe_id' => $recipe->id,
                    'name' => $faker->word,
                    'qty' => mt_rand(1,12).' Kg'
                ]);
            }
            foreach (range(1, mt_rand(5,12)) as $k){
                RecipeDirection::create([
                    'recipe_id' => $recipe->id,
                    'description' => $faker->sentence
                ]);
            }
        }
    }
}

运行迁移

>php artisan migrate
>php artisan db:seed
//php artisan migrate:refresh
//php artisan db:seed

你可能感兴趣的:(Recipe-box 数据填充)