如何使用 Laravel 迁移将 DATE 列的默认值设置为当前日期?

问题描述 投票:0回答:0

我在创建迁移时遇到了一些问题,我在表中的列之一是 DATE 类型,而不是 DATETIME 或 TIMESTAMPS。 问题是我找不到定义此列默认值的方法,我尝试了以下方法:

$table->date('request_date')->default(Carbon::now());
//This uses the date the migration was created as a default, not the current date.

$table->date('request_date')->default(DB::raw('CURRENT_DATE()'));

$table->date('request_date')->default(DB::raw('date(now())'));

$table->date('request_date')->default(DB::raw('CURRENT_DATE'));

使用 DB:raw 给我一个错误:

SQLSTATE[42000]: Syntax error or access violation: Doctrine\DBAL\Driver\PDO\Exception::("SQLSTATE[42000]: Syntax error or access violation: 1064 你的SQL语法有错误;检查手册对应于您的 MySQL 服务器版本,以便在

issue_date
之后的 'CURRENT_DATE() 附近使用正确的语法,在第 1 行的 `request_da' 之后添加
expiry_date
date null")

任何建议将不胜感激,谢谢!

laravel laravel-8 laravel-migrations
© www.soinside.com 2019 - 2024. All rights reserved.