我使用laravel的Seeder的工厂模式来填充数据
$factory->define(App\Models\User::class, function (Faker\Generator $faker) {
static $password;
return [
'username' => $faker->name,
'email' => $faker->unique()->safeEmail,
'password' => $password?: $password = bcrypt('secret'),
'remember_token' => str_random(10),
'created_at' => Carbon\Carbon::now(),
'updated_at' => Carbon\Carbon::now(),
];
});
但是使用命令的时候显示
[PDOException]
SQLSTATE[HY000]: General error: 1364 Field 'password' doesn't have a default value
我的第一反应是没有设置 $filable password字段,但是我的User.model设置了,使用tinker单个填充的时候也是可以的,就是使用工厂模式批量填充,缺少password,报错部分
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1364 Field 'password' doesn't have a default value (SQL: insert into
`sw_users` (`created_at`, `email`, `updated_at`, `username`) values (2017-02-24 10:34:56, parker00@ex
ample.org, 2017-02-24 10:34:56, Prof. Cheyenne Welch), (
很奇怪为什么password,没有生成成功,希望站长能帮忙解答下,谢谢