【问题标题】:Changes to Eloquent model+migration not working更改 Eloquent 模型+迁移不起作用
【发布时间】:2017-10-01 09:23:32
【问题描述】:

我正在为我的 Laravel 5.4 项目添加一些身份验证。但是,当更新提供的 User.php 文件(这是一个 Eloquent 模型)的 $fillable 数组以对应于我的新用户表迁移时,注册新用户时的插入查询不会传播更改。见下文。

    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'api_token' => str_random(60),
            'password' => bcrypt($data['password'])
        ]);
    }

我已将“api-token”添加到我的数据库中,并更新了我的模型的 $fillable 数组

    protected $fillable = [
        'name', 'email', 'password', 'api-token',
    ];

我的迁移已通过添加“api-token”字段的更改刷新

Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->string('api_token', 60)->unique();
            $table->rememberToken();
            $table->timestamps(); 
        });

但是我仍然遇到同样的错误,即:

null value in column "api_token" violates not-null constraint

(SQL: insert into "users" ("name", "email", "password", "updated_at", "created_at") values (Bradley, foo@bar.com, $2y$10$IBq0yEumngpeWf/oql1HgeMRYP1ANj.owXqkMc7gtIN/kxcW46Giu, 2017-05-03 13:37:56, 2017-05-03 13:37:56) returning "id")

该错误清楚地表明在插入新用户时甚至没有考虑api_token字段。我是否错过了对模型进行更改以使其生效后需要对 Laravel 进行的某种更新?

【问题讨论】:

  • 我将此问题标记为关闭,因为这是一个简单的印刷错误,不太可能帮助其他人

标签: laravel laravel-5 eloquent


【解决方案1】:

在您的可填写项中添加了api-token,而在迁移和创建中您使用了api_token
注意-_ 之间的区别。

【讨论】:

  • 我很惭愧没有注意到这一点。
猜你喜欢
  • 1970-01-01
  • 2016-05-14
  • 2012-09-08
  • 2016-07-18
  • 1970-01-01
  • 2016-10-28
  • 1970-01-01
  • 2023-03-14
  • 2019-08-11
相关资源
最近更新 更多