user1876234 user1876234 - 5 months ago 32
SQL Question

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');
$nerd->save();

Answer

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

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