【问题标题】:default() is not working in laravel migrationdefault() 在 laravel 迁移中不起作用
【发布时间】:2020-10-25 06:57:48
【问题描述】:

我正在使用 laravel 创建一个待办事项列表,并且我有一个包含以下列的表格:

'title', 'description', '完成', 'id'

每当我向表中添加新任务时,都会自动设置“已完成”列,其默认值为 0,其编码如下:

$table->boolean('completed')->default(0);

此列工作得很好,但是当我尝试给“描述”一个默认值(通过为描述留下一个空输入)时,它给了我一个“违反完整性约束:1048 列“描述”不能为空”错误.

我为“描述”列编写的代码如下:

$table->string('description')->default('-');

我尝试了 text() 数据类型,但它给了我同样的错误。

我也尝试对此列使用 nullable(),但默认值 '-' 没有添加到列中,它返回一个空值。

我用来添加值的表单如下所示:

<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>

我的控制器存储方法:

public function createTask(validation $request){
        $userId = auth()->id();
        $request['user_id'] = $userId;
        Task::create($request->all());
        return redirect()->route('showList');
    }

和表创建语句:

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

【问题讨论】:

  • 请分享show create table您的表格的语句结果(请编辑问题,不要作为评论发表)
  • 你试过空字符串吗?喜欢" "。您是否每次都更改表格或重新创建表格?如果要通过迁移对其进行更改,则需要将 -&gt;change(); 附加到这些定义中。
  • 可以分享一下存储方法吗?只是为了确保您没有手动将描述值添加为空字符串
  • @HassanAzzam 当然,我现在刚刚编辑了问题
  • @Ersoy 当然,现在刚刚添加了 create 语句 :)

标签: php laravel


【解决方案1】:

在看到 create table 语句和你的 store 函数之后。看起来您的商店功能更有可能是这里的问题。试试这个:

use Illuminate\Support\Facades\Auth;

public function createTask(Request $request){
    $user = Auth::user();//get the user model
    $request->validate([//validate that the data is according to limitations
        'title' => 'required|max:255',
        'description' => 'max:1000',
    ]);
    $task = new Task;  //create a new instance of Task
    //fill in the data
    $task->title = $request->input('title');
    if(!empty($request->input('description')){
        $task->description = $request->input('description');
    }
    $task->user_id = $user->id;
    $task->save();//save it
    
    return redirect()->route('showList');
}

在您的代码中,您使用详细描述的质量分配here

对于批量分配,您应该在 Modelprotected $fillable = ['title','description','user_id']; 中将数据库字段定义为可批量分配

您的具体问题是 laravel 根据此问题的答案将空输入设置为 null:Mass assignment won't handle Null input even when default is set on migration.Any solution to this?

您应该始终验证用户输入,而 laravel 提供了强大的工具来做到这一点:https://laravel.com/docs/7.x/validation

您可能还想考虑使用 laravel 内置的默认值,或者至少检查它们是否干预数据:How to set the default value of an attribute on a Laravel model

【讨论】:

  • 非常感谢!你的回答完全解决了我的问题。问题是 store 函数将空值作为“null”传递给 description 列,而 default() 唯一起作用的地方是没有值传递给数据库的时间。再次感谢!
猜你喜欢
  • 2017-06-18
  • 2018-06-18
  • 2021-02-08
  • 2018-01-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多