进入database下面的factory
$factory->define(User::class, function (Faker $faker) {
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'remember_token' => Str::random(10),
];
});
$factory->define(AppModelsPost::class, function (Faker $faker) {
return [
'title' => $faker->sentence(6),
'content' => $faker->paragraph(10),
];
});
然后进入目录使用tinker
php artisan tinker
执行
factory(AppModelsPost::class,50)->make()
//展示不创建 要创建到数据库使用create
laravel 中使用faker填充数据
2017.07.23 21:29:09字数 55阅读 402
1.在
databasefactoriesModelFactory.php
中添加如下代码
//数据填充
$factory->define(AppPost::class, function (FakerGenerator $faker) {
return [
'title' => $faker->sentence(6),
'content' => $faker->paragraph(10)
];
});
Post 是一个表模型 在return中设置要填充的数据
具体参考https://github.com/fzaninotto/Faker
2.在终端中输入php artisan tinker
进入tinker
//查看带生成的数据不会添加到数据库
factory(AppPost::class,10)->make()
//插入到数据库
factory(AppPost::class,10)->create()
## AppPost是表模型的命名空间位置,10表示生成多少条