对Eloquent模型+迁移的更改无效

时间:2017-05-03 13:42:41

标签: laravel laravel-5 eloquent

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

    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进行的某些更新,以使其生效?

1 个答案:

答案 0 :(得分:1)

在您的论坛中,您在迁移和创建时添加了api-token,您使用了api_token 请注意-_之间的区别。