user1876234
user1876234

Reputation: 857

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();

Upvotes: 3

Views: 24425

Answers (4)

Ziaur Rahman
Ziaur Rahman

Reputation: 1188

I have faced the same problem. This value is not null in database and from front end it send a null value. The error is shown below

"Integrity constraint violation: 1048 Column 'MoneyMethod' cannot be null".

My view page input tag:

{!! Form::select("MoneyMethod", $MoneymethodInfo, null,["class"=>"form-control MoneyMethod required","id"=>"MoneyMethod"]) !!},

notice the spelling of 'MoneyMethod' in my database table $table->string('MoneyMethod', 100); the spelling of money method is same as view page but in my controller

$riskfund->Moneymethod  = Input::get('Moneymethod');

look carefully the spelling of 'Moneymethod' differs from view page and database table column. After correcting the spelling, it is now getting the value.

So, please check the spelling of 'username' in form, controller, database table and also notice that the database field is nullable or not.

Upvotes: -1

miken32
miken32

Reputation: 42766

This error may also occur if a field is not included in the model's $fillable property. For example, with this code:

class User extends Model {
    protected $fillable = ["name", "email", "phone"];
}

$user = User::create([
    "name" => "Joe Bloggs",
    "email" => "[email protected]",
    "address" => "14 Sutton Drive, Dublin 13",
    "phone" => "0866582753",
]);

The address value will not be passed to the database query, which may result in the 1048 error. (If the address column is declared nullable, no error will be thrown, but the column will also never be updated by any mass assignment methods.)

Upvotes: 0

Dave
Dave

Reputation: 3658

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

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

Or, in more recent Laravel versions:

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

Upvotes: 5

yogurt
yogurt

Reputation: 380

You can use model events (http://laravel.com/docs/eloquent#model-events).

When a Contact is creating or updating, you can test if username is null, and change it to '' before writing it to DB.

Upvotes: 1

Related Questions