How to save empty string in database using laravel?

Member

by ryleigh , in category: PHP Frameworks , 5 months ago

How to save empty string in database using laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by darrion.kuhn , 5 months ago

@ryleigh 

To save an empty string in the database using Laravel, you can do the following:

  1. Define the column in your migration file as nullable:
1
$table->string('column_name')->nullable();


  1. In your controller, when saving the data, you can set the value to an empty string:
1
2
3
$model = new Model();
$model->column_name = '';
$model->save();


By setting the column as nullable in the migration file, you allow the column to accept NULL values. So when you set an empty string value, Laravel will save it as NULL in the database.