Laravel 1048 Column cannot be NULL on storing data


When I’m trying to save a new contact I’m getting 1048 Column 'username' cannot be NULL error. It’s pretty obvious that cause of this error is empty value of username, however I’d like to make it work without setting a column to NULL or checking if username POST data is empty and then setting it’s value to ''.

I’ve seen numerous of examples, where column is not set to NULL and/or data is not set to '' before saving to database.

Or maybe I’m wrong and I missed something ?

I hope someone could comment on that..

$contact = new Contact;
$contact->name      = Input::get('name'); 
$contact->username  = Input::get('username');


Set default non-null values for the Input variable(s).

$contact = new Contact;
$contact->name     = Input::get('name');
$contact->username = Input::get('username', '');

Or, in more recent Laravel versions:

$contact = new Contact;
$contact->name     = $request->input('name');
$contact->username = $request->input('username', '');

Answered By – Dave

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published