orm 查詢出的時間格式為 2023-07-14T01:46:23.000000Z 怎么格式化成 2023-07-14 01:46:23呢
這里寫搜到的方案及不適用原因
在 Laravel 框架中,你可以通過覆蓋模型的 serializeDate
方法來自定義日期序列化的格式。你提供的代碼示例正是這樣做的。
在 webman 框架中,由于它也使用了 Eloquent ORM,
namespace app\common\traits;
use Carbon\CarbonInterface;
use DateTimeInterface;
trait DateFormat
{
protected function serializeDate(DateTimeInterface $date): string
{
return $date->format($this->dateFormat ?: CarbonInterface::DEFAULT_TO_STRING_FORMAT);
}
}
namespace app\Model;
use Illuminate\Database\Eloquent\Model;
use app\common\traits\DateFormat;
class AdminModel extends Model
{
use DateFormat;
// 其他代碼...
}
這樣設定無效 不知道為什么
在 Eloquent 模型上使用 toArray 或 toJson 方法時,Laravel 7 將使用新的日期序列化格式。為了格式化日期以進行序列化,Laravel 將會使用 Carbon 的 toJSON 方法,該方法將生成與 ISO-8601 兼容的日期,包括時區(qū)信息及小數(shù)秒。此外,該更改提供了更好的支持,并與客戶端日期解析庫集成。
此前,日期將序列化為以下格式:2020-03-04 16:11:00 。使用新格式進行序列化的日期將顯示為:2020-03-04T20:01:00.283041Z
如果你希望繼續(xù)保持之前所用的格式,你可以重寫模型的 serializeDate 方法:
/**