php - Mysql 不接受表单输入 Laravel 5.2

标签 php mysql laravel

PHP 和 Laravel 5.2 新手。我使用 php artisan make:auth 创建了一个用户表,但想在我的数据库中添加两列。我能够在数据库中添加列,但是当我注册新用户时,它接受除 mysql 中的名字和姓氏以外的所有内容,这些列显示为空白。我不明白我错过了什么?

    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('first_name');
        $table->string('last_name');
        $table->string('email')->unique();
        $table->string('avatar')->default('default.jpg');
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
        });

我的 AuthController 文件

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


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

register.blade 上名字的表单输入示例

<div class="form-group{{ $errors->has('first_name') ? ' has-error' : '' }}">
                        <label class="col-md-4 control-label">First Name</label>

                        <div class="col-md-6">
                            <input type="text" class="form-control" name="first_name" value="{{ old('first_name') }}">

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

最佳答案

正如 @Sid 所说,这些字段需要可批量分配,这在文档中进行了描述:

You may also use the create method to save a new model in a single line. [...] before doing so, you will need to specify either a fillable or guarded attribute on the model, as all Eloquent models protect against mass-assignment.

来自https://laravel.com/docs/5.2/eloquent#mass-assignment

class Flight extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name'];
}

关于php - Mysql 不接受表单输入 Laravel 5.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37640038/

相关文章:

php - 将数组的所有索引放在逗号分隔的字符串中

处理多个服务器的 PHP session

php - Sql注入(inject)获取id解决方案

当有 '('但没有 ')'时mysql查询错误

javascript - 如何使用ajax发送图像和数据

mysql - 带有 where 子句 IN 的 SELECT 语句不读取 MySQL 中的变量

sql - 从日期... SQL 的数据库中删除数据

php - Python无法连接MySQL

mysql - Eloquent 不会更新 MySQL 中的所有记录

php - Laravel:有几个种子表属于使用工厂的关系