首页 > 解决方案 > Laravel 创建日期返回不同的格式

问题描述

我尝试从数据库中选择create_data,数据库中的日期格式类似于“2021-05-18 11:06:01”,但是当我检索并显示它时,它会显示类似于“2021-05”的格式-18T03:06:01.000000Z”。有谁知道为什么会变成这样?我想要实际上类似于数据库存储的日期,即“2021-05-18 11:06:01”。但是,数据库中的 create_date 格式为 UTC +8,但接收时会显示 UTC 格式。

在此处输入图像描述

数据返回

[0] => Array (
    [id] => 1
    [log_name] => login
    [description] => login
    [subject_type] => App\Models\Users
    [subject_id] => 0
    [causer_type] => App\Models\User
    [causer_id] => 2
    [properties] => ""         
    [created_at] => 2021-05-18T03:06:01.000000Z
);

代码

$lastLoggedActivity = ActivityLog::where('causer_id', $userid)
    ->orWhereIn('subject_id', $selectparentid)
    ->with('getLogType')
    ->with('getCauserDetails')
    ->orderBy('created_at', 'desc')
    ->get()
    ->toArray();

标签: phplaraveleloquent

解决方案


在 ActivityLog 模型中添加这个,

use Carbon\Carbon;

public function getCreatedAtAttribute($value)
{
    return Carbon::parse($value)->format('Y-m-d H:i:s');
}

推荐阅读