1.编辑 /database/factories/ModelFactory,添加新的类模型填充

$factory->define(App\Post::class, function (Faker\Generator $faker) {
    return [
      //会生成一个新的User,可以改为常量或者rand(1,10)
'user_id' => factory(App\User::class)->create()->id, 'title' => $faker->sentence, 'body' => $faker->paragraph, ]; });

2.CLI测试

php artisan tinker
factory('App\Post')->make();
factory('App\Post', 5)->make();

也可在tinker下create()直接添加测试数据

//数据库插入100条数据
factory('App\Post', 100)->create();

3.编辑 /database/seeds/DatabaseSeeder 

class DatabaseSeeder extends Seeder
{
    public function run()
    {
        Model::unguard();

        factory(App\Product::class, 50)->create();

        Model::reguard();
    }

4.CLI调用

php artisan db:seed

查询数据库将会看到插入了新的数据

 

5.关于facker的各种数据类型,可查看

https://github.com/fzaninotto/Faker

posted on 2015-10-05 15:27  jzfan  阅读(340)  评论(0编辑  收藏  举报