genetess aquino genetess aquino - 1 month ago 58
PHP Question

Laravel 5.3: Cannot add Additional user info for User registration

Im trying to add a new registration detail for the user Which is the user's LASTNAME. But Im having trouble in inserting it to my database. This is the error that comes up

ERROR:


QueryException in Connection.php line 761:
SQLSTATE[HY000]: General error: 1364 Field 'f_lastname' doesn't have a default value (SQL: insert into
users
(
name
,
email
,
password
,
updated_at
,
created_at
) values (chu, chu@yahoo.com, 111, 2016-10-12 06:55:33, 2016-10-12 06:55:33))


This is my database migration file

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


The user.php $fillable code

class User extends Authenticatable


{
use Notifiable;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'f_lastname', 'email', 'password',
];

/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];


}

and the RegistrationController.php validator section

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

/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'f_lastname' => $data['f_lastname'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}


Most of the tutorials I saw was for laravel 5.2. Im just new in using laravel 5.3. Appreciate your help guys!

Answer

Error is self explanatory.

It states the column f_lastname is not null and there is no default value assigned to it. So in your query you have to include that column with some value or change the table structure and assign some default value to it or make it null accepted column.

Ex :

Change the column to allow null:

ALTER TABLE `user` CHANGE `f_lastname` `f_lastname` TEXT NULL

or, give it a default value as empty string:

ALTER TABLE `user` CHANGE `f_lastname` `f_lastname` TEXT NOT NULL DEFAULT ''