General Error : Field doesn’t have a default value in Laravel

This is one of the most common problem, developers face while working with Laravel.

SQLSTATE[HY000]: General error: 1364 Field ‘username’ doesn’t have a default value (SQL: insert into `users` (`name`, `email`, `password`, `updated_at`, `created_at`) values (laravel-nepal, [email protected], $2y$10$JMnqvKTSHDnai0/UpDlNG.bT693u5ZyBM4MiFKYsJrPv1obhwyfoq, 2019-08-25 04:29:37, 2019-08-25 04:29:37))

Some of the common reasons to come across this problem are :

  1. We might not be sending the username from our input form ( FormRequest may not have the username key )
  2. Fillable property may not have the username field.

How do we solve the problem ?

First make sure we do have username on our request. Do dd() on the request and if we have the username and still the errors is there then we may not have appended the username field on our $fillable property in our model.

<?php
namespace App;
use IlluminateNotificationsNotifiable;
use IlluminateContractsAuthMustVerifyEmail;
use IlluminateFoundationAuthUser as Authenticatable;
class User extends Authenticatable
{
    use Notifiable;
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'username', // username added
    ];
}

Hope this tips was helpful for you 🙂 Happy coding 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest