larevel数据填充

  • 进入文件app/database/factorise/UserFactory.php,可以看到复制已有的一份
    $factory->definde(AppPost::class,function(Faker $faker){
        return [
            'title' => $faker->sentence('6'),//可以查看网址https://github.com/fzaninotto/Faker
            'content' => $faker->paragraph('10')
        ];
    });
    //App/Post是指Post模型的命名空间
    //return 返回的字段是数据库中的字段,created_at updated_at不需要填充,可以自动增加;id为自增,也不需要进行填充
  • 进入tinker页面:php artisan tinker
  • 使用factory(AppPost::class,10)->make();//第二个参数为填充数据的条数,AppPost模型的命名空间,;必须要加上,make()方法只打印在页面上,不插入数据库,如果插入数据库可以使用create()
原文地址:https://www.cnblogs.com/hanmengya/p/10857817.html