【问题标题】:How can I connect my additional field to database on Laravel 5.4?如何将我的附加字段连接到 Laravel 5.4 上的数据库?
【发布时间】:2017-02-23 18:25:30
【问题描述】:

我已经在注册表单上尝试了我的代码。 当我单击注册按钮时,会出现一些错误。 你能帮我解决这个问题吗? Register Form Screenshoot

Error Message Screenshoot

迁移创建用户表

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

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('users');
}

RegisterController.php

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

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return User
 */
protected function create(array $data)
{
    $fields = [
        'name'     => $data['name'],
        'email'    => $data['email'],
        'password' => bcrypt($data['password']),
        'phone'    => $data['phone'],
    ];
    if (config('auth.providers.users.field','email') === 'username' && isset($data['username'])) {
        $fields['username'] = $data['username'];
    }
    return User::create($fields);
}

然后在我的注册表单上:

<div class="form-group has-feedback">
   <input type="text" class="form-control" placeholder="Phone Number" name="phone" value="{{ old('phone') }}"/>
   <span class="glyphicon glyphicon-phone form-control-feedback"></span>
</div>

【问题讨论】:

  • 你能发布你的模型代码吗?
  • 您可以在User::create 之前执行dd($fields) 并将输出与您的型号代码一起发布吗?

标签: php laravel localhost laravel-5.3


【解决方案1】:

就我个人而言,在 Schema 中,我会将“电话”字段设为 -&gt;nullable(),并添加 -&gt;default(null)。然后,您可以从 RegisterController.php 文件中删除 'phone' =&gt; 'required' 验证。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-15
    • 1970-01-01
    • 2018-01-05
    • 2018-12-07
    • 2017-10-03
    • 1970-01-01
    • 2020-12-03
    相关资源
    最近更新 更多