Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

NikhilLVL's avatar

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'address' cannot be null

I was trying to update my table details. Here's my controller: $data=Detail::find($id); $data->company_name=request('new_company_name'); $data->address=request('new_address'); $data->place=request('new_place'); $data->country=request('new_country'); $data->company_email=request('new_company_email'); $data->company_phone=request('new_company_phone'); $data->save();

But getting above error. How to solve this ?

1 like
2 replies
tisuchi's avatar
tisuchi
Best Answer
Level 70

@nikhillvl you have to make sure that new_address has some value.

Alternatively, you can set the address field as nullable in the table.

Or, you can use like that also-

$data=Detail::find($id); 
$data->company_name=request('new_company_name'); 
$data->address= request('new_address') ?? ''; 
$data->place=request('new_place'); 
$data->country=request('new_country'); 
$data->company_email=request('new_company_email');
$data->company_phone=request('new_company_phone'); 
$data->save();
6 likes
rodrigo.pedra's avatar

Well your table is defined to not accept null values in the address column.

And most probably when the user is submitting the form you are handling, they are not filling anything on the new_adress field.

You can either:

  1. Add validation to prevent blank new_adress values
  2. Add a default value (could be an empty string) when retrieving the new_address value from the request.

Option 1 would something like this:

request()->validate([
    'new_address' => ['required'],
    // add rules for other fields
]);

$data = Detail::find($id);
$data->company_name = request('new_company_name');
$data->address = request('new_address');
$data->place = request('new_place');
$data->country = request('new_country');
$data->company_email = request('new_company_email');
$data->company_phone = request('new_company_phone');
$data->save();

You can add similar validation rules to any other require field. When any validation rule fails, the user is redirected back to the form, and you can show the user validation errors.

You can read more about Validation, and how to display validation errors, on the docs:

https://laravel.com/docs/8.x/validation

Option 2 would something like this:

$data = Detail::find($id);
$data->company_name = request('new_company_name');
// second parameter is the default value
// in this case an empty string
$data->address = request('new_address', '');
$data->place = request('new_place');
$data->country = request('new_country');
$data->company_email = request('new_company_email');
$data->company_phone = request('new_company_phone');
$data->save();

Note I just added a default value for the column the error message is referring to. Add default values to any columns that don't accept NULL.

The default value can be anything you want, if you find an empty string misleading you could try something like this:

$data->address = request('new_address', 'Not informed');

Hope it helps.

5 likes

Please or to participate in this conversation.