使用工厂函数添加假数据、报字段没有默认值?
工厂函数代码
$factory->define(User::class, function (Faker $faker) {
$date_time = $faker->date . ' ' . $faker->time;
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'email_verified_at' => now(),
'password' => '$2y$10$6BixObQ108Da47eYT4RNa.SrgHIiL/8PI/roaj9JKlejscDyiXR0q', // password
'remember_token' => Str::random(10),
'created_at' => $date_time,
'updated_at' => $date_time,
];
});
制作假数据 tinker
factory(App\User::class)->times(5)->make();
Illuminate\Database\Eloquent\Collection {#3057
all: [
App\User {#3053
name: "Joan Beahan Sr.",
email: "nikolaus.verda@example.com",
email_verified_at: "2020-07-16 07:27:25",
created_at: "1992-08-23 21:25:25",
updated_at: "1992-08-23 21:25:25",
},
...
执行迁徙后报错
php artisan migrate:fresh --seed
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1364 Field 'password' doesn't have a default value (SQL: insert into `users` (`created_at`, `email`, `email_verified_at`, `name`, `updated_at`) values (1987-09-24 10:12:24, augustus.wilkinson@example.net, 2020-07-16 07:29:12, Broderick Langworth, 1987-09-24 10:12:24), (2008-07-09 18:57:02, beatrice.carroll@example.com, 2020-07-16 07:29:12, Jayce Schuster, 2008-07-09 18:57:02), (1992-10-26 01:30:30, bailey.merl@example.org, 2020-07-16 07:29:12, Lemuel Sipes III, 1992-10-26 01:30:30), (1985-05-17 12:48:19, kristopher.walter@example.net, 2020-07-16 07:29:12, Prof. Markus Howe, 1985-05-17 12:48:19), (1981-04-01 21:49:44, rodger.frami@example.org, 2020-07-16 07:29:12, Elbert Russel MD, 1981-04-01 21:49:44))
at /var/www/html/bianca/vendor/laravel/framework/src/Illuminate/Database/Connection.php:669
665| // If an exception occurs when attempting to run a query, we'll format the error
666| // message to include the bindings with SQL, which will make this exception a
667| // lot more helpful to the developer instead of just the database's errors.
668| catch (Exception $e) {
> 669| throw new QueryException(
670| $query, $this->prepareBindings($bindings), $e
671| );
672| }
673|
Exception trace:
1 PDOException::("SQLSTATE[HY000]: General error: 1364 Field 'password' doesn't have a default value")
/var/www/html/bianca/vendor/laravel/framework/src/Illuminate/Database/Connection.php:463
显示Field 'password' doesn't have a default value
这是为什么呢
推荐文章: