I am creating a to do list using laravel, and I have a table with the columns below:
'title', 'description', 'completed', 'id'
The 'completed' column is set automatically whenever I add a new task to my table, its default value is 0 and it's being coded like this:
$table->boolean('completed')->default(0);
This column works perfectly fine, but when I try to give 'description' a default value (by leaving an empty input for description), it gives me a 'Integrity constraint violation: 1048 Column 'description' cannot be null' error.
The code I have written for 'description' column is the code below:
$table->string('description')->default('-');
I tried the text() data type, but it gives me the same error.
I also tried using nullable() for this column, but the default value, '-' , doesn't get added to the column and it returns a emtpy value.
The form I'm using to add the values looks like this:
<form action="/create" class="panel" method="post">
@csrf
<input name="title" type="text" class="title">
<textarea name="description" class="title"></textarea>
<input type="submit" class="submit">
</form>
My controller store method:
public function createTask(validation $request){
$userId = auth()->id();
$request['user_id'] = $userId;
Task::create($request->all());
return redirect()->route('showList');
}
and the table create statement:
CREATE TABLE `tasks` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`completed` tinyint(1) NOT NULL DEFAULT 0,
`user_id` bigint(20) unsigned NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`description` text COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT ' ',
PRIMARY KEY (`id`),
KEY `tasks_user_id_foreign` (`user_id`),
CONSTRAINT `tasks_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci