Laravel5.6 Factory 生成測試數據
一、生成模型工廠
# 建立模型工廠
php artisan make:factory PostFactory
# 配置模型
use Faker\Generator as Faker;
$factory->define(App\Models\User::class, function (Faker $faker) {
return [
'username' => $faker->unique()->username,
'email' => $faker->unique()->safeEmail,
'mobile' => $faker->unique()->phoneNumber,
'password' => bcrypt('123456'),
'nickname' => $faker->unique()->name,
'reg_ip' => app('request')->ip(),
'last_login_ip' => app('request')->ip(),
'remember_token' => str_random(10),
];
});
二、生成數據
# 進入命令行
php artisan tinker
# 寫入數據 make 生成數據 create 生成並寫入到數據庫
factory(App\Models\User::class, 5)->create();
三、生成中文數據
# 配置 app.php 添加如下配置項
'faker_locale' => 'zh_CN'