使用工厂函数添加假数据、报字段没有默认值?

工厂函数代码

$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 这是为什么呢

《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

seeds 文件加一个设置看看:

$users = $users->makeVisible(['password']);
4年前 评论
讨论数量: 4

注意看看报错信息:

SQL: insert into `users` (`created_at`, `email`, `email_verified_at`, `name`, `updated_at`)

这里根本没有插入 password 字段,看看是不是没有加入 $fillable

4年前 评论
cccyzloong (楼主) 4年前

seeds 文件加一个设置看看:

$users = $users->makeVisible(['password']);
4年前 评论
leo

seed 文件怎么写的?

4年前 评论
cccyzloong (楼主) 4年前
cccyzloong (楼主) 4年前

@leo 看代码就知道是根据 Summer 老大的教程写的(因为我刚学了不久,还有印象),应该就少了一句:

$user_array = $users->makeVisible(['password', 'remember_token'])->toArray();

主要是 password 字段他还改为了不允许为空,所以就报错了 :smile: :smile:

4年前 评论
cccyzloong (楼主) 4年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!