laravel没有将自定义值保存到用户tabl

时间:2017-07-06 19:34:34

标签: php mysql database laravel

我正在使用Laravel 5.4和MySql数据库。 我正在尝试扩展我的用户表并在注册用户时保存更多信息,但看起来该值无法保存到数据库中。

这是我的迁移(只有up方法):

Schema::table('users', function (Blueprint $table) {
    $table->string('mobile_phone')->after('remember_token');
});

我已将mobile_phone元素放入$ fillable数组中:

protected $fillable = [
    'name', 'email', 'password', 'mobile_phone'
];

并将值添加到app / Http / Controllers / Auth / RegisterController.php

protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => 'required|string|max:255',
        'email' => 'required|string|email|max:255|unique:users',
        'mobile_phone' => 'required|string|unique:users',
        'password' => 'required|string|min:6|confirmed',
    ]);
}

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

在表单中添加了输入:

<div class="form-group{{ $errors->has('mobile_phone') ? ' has-error' : '' }}">
    <label for="mobile_phone" class="col-md-4 control-label">Mobile</label>
    <div class="col-md-6">
        <input id="mobile_phone" type="text" class="form-control" name="mobile_phone" value="{{ old('mobile_phone') }}" required>

        @if ($errors->has('mobile_phone'))
            <span class="help-block">
                <strong>{{ $errors->first('mobile_phone') }}</strong>
            </span>
        @endif
    <div>
</div>

如果我在config / database.php中将'strict'的值设置为true,则会返回此错误:

SQLSTATE[HY000]: General error: 1364 Field 'mobile_phone' doesn't have a default value (SQL: insert into `users` (`name`, `email`, `password`, `updated_at`, `created_at`) values (Loki, loki@loki.com, $2y$10$9Y1HtCIHLThiwRNklz5WKOT7Y/B0Prpvb9yMJRmlGePhPrLDNFDme, 2017-07-06 19:32:09, 2017-07-06 19:32:09))

当我将该值设置为false时,它会保存新用户,但mobile_phone列为空。

我错过了什么?

非常感谢

0 个答案:

没有答案