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>
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'];
}