0

Newbie to PHP and Laravel 5.2. I created a users table with php artisan make:auth, but want to add two more columns to my database. I was able to add the columns in the database, but when I register new users it accepts everything but first_name and last_name in mysql which come up blank. I don't understand what I'm missing?

    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();
        });

My AuthController file

   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']),
    ]);

Example of form input for first_name on 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>
3
  • have you added first_name and last_name to fillable array in User model? Commented Jun 5, 2016 at 8:54
  • No I forgot to do that. I'll check that out and get back to you on that Commented Jun 5, 2016 at 20:41
  • That did the trick! Thanks Sid Commented Jun 6, 2016 at 0:59

1 Answer 1

1

As @Sid stated, these fields need to be mass-assignable, which is described in the docs:

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.

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

class Flight extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name'];
}
Sign up to request clarification or add additional context in comments.

1 Comment

I'll try this out as well, thanks for the suggestions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.