Right, so I have been getting this error for some time now:
Illuminate\Database\QueryException SQLSTATE[HY000]: General error: 1364 Field 'username' doesn't have a default value (SQL: insert into
users
(name
,updated_at
,created_at
) values (ggg, things@see.com, 2019-12-15 16:58:26, 2019-12-15 16:58:26))
I don't want to set username to nullable
, as I need to have it. This is how the table looks like:
mysql> desc users;
+-------------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| email | varchar(255) | NO | UNI | NULL | |
| username | varchar(255) | NO | UNI | NULL | |
| email_verified_at | timestamp | YES | | NULL | |
| password | varchar(255) | NO | | NULL | |
| remember_token | varchar(100) | YES | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+-------------------+---------------------+------+-----+---------+----------------+
9 rows in set (0.00 sec)
This is how the create method looks like:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'username' => $data['username'],
]);
}
That's where I am getting the error.
EDIT:
Okay, so I fixed it by moving the 'username'
attribute into the $fillable
array. I also copied the original App/User
class that Laravel provides and just added my methods to it.
null
name. You should check the process that writes to the table.