久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何將 Laravel 輸出的日期格式更改為 JSON?

How do I change the date format Laravel outputs to JSON?(如何將 Laravel 輸出的日期格式更改為 JSON?)
本文介紹了如何將 Laravel 輸出的日期格式更改為 JSON?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我在 Laravel 中構建了一個應用程序,并以這種格式 eloquent 返回日期:2015-04-17 00:00:00.我正在向 JSON 發送一個特定查詢,以便我可以使用 D3 制作圖表,并且我想我想要 ISO8601 ('1995-12-17T03:24:00') 中的日期或一些與 javascript Date() 構造函數配合使用的其他格式.

I've built an application in Laravel and eloquent returns dates in this format: 2015-04-17 00:00:00. I'm sending one particular query to JSON so I can make a graph with D3, and I think I would like the dates in ISO8601 ('1995-12-17T03:24:00') or some other format that plays nice with the javascript Date() constructor.

有沒有辦法在 Laravel 端將輸出的日期格式更改為 JSON?我不確定使用 mutator 是最好的方法,因為它會影響我應用程序其他部分的日期.

Is there a way to change the date format being output to JSON on the Laravel end? I'm not sure using a mutator is the best approach because it would affect the date in other parts of my application.

或者最好保留 JSON 輸出,并在將日期格式傳遞給 Date() 構造函數之前使用一些 javascript 字符串方法來操作日期格式?哪種方法更有效?

Or would it be better to leave the JSON output as is, and use some javascript string methods to manipulate the date format before passing it to the Date() constructor? Which approach is more efficient?

這是我的模型:

class Issue extends Model {



protected $fillable = [
    'client_id',
    'do',
    'issue_advocate',
    'service_number',
    'issue_location',
    'issue_description',
    'level_of_service',
    'outcome',
    'referral_id',
    'file_stale_date',
    'date_closed',
    'issue_note',
    'staff_hours'
];

protected $dates = [
    'do',
    'date_closed',
    'file_stale_date'
];

public function setDoAttribute($value)
{
    $this->attributes['do'] = Carbon::createFromFormat('F j, Y', $value)->toDateString();
}
}

這是我的查詢:

$issues = Issue::with('issuetypes')
->select(['do','level_of_service','outcome','id'])
->whereBetween('do',[$lastyear,$now])
->get()->toJson();

以及我返回的 JSON:

And the JSON I get back:

[{"do":"2014-12-23 00:00:00","level_of_service":1,"outcome":1,"id":18995,"issuetypes":[{"id":9,"issuetype":"Non Liberty","pivot":{"issue_id":18995,"issuetype_id":9}}]}]

推薦答案

我知道這是一個老問題,但仍然沒有好的答案.更改protected $dateFormat 會影響數據庫,而必須重寫方法serializeDate()

I know it's an old question, but there is still no good answer to that. Changing protected $dateFormat will affect database, instead method serializeDate() must be overriden

class MyModel extends Eloquent {
    protected function serializeDate(DateTimeInterface $date) {
        return $date->getTimestamp();
    }
}

或者我自己我選擇創造特質

Or myself I chose to create trait

trait UnixTimestampSerializable
{
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->getTimestamp();
    }
}

然后添加

class SomeClassWithDates extends Model {    
    use UnixTimestampSerializable;

    ...
}

這篇關于如何將 Laravel 輸出的日期格式更改為 JSON?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

add new element in laravel collection object(在 Laravel 集合對象中添加新元素)
Creating an edit modal in Laravel 5(在 Laravel 5 中創建編輯模式)
Laravel 5.5 API resources for collections (standalone data)(用于集合的 Laravel 5.5 API 資源(獨立數據))
What is the best practice to create a custom helper function in php Laravel 5?(在 php Laravel 5 中創建自定義輔助函數的最佳實踐是什么?)
No #39;Access-Control-Allow-Origin#39; header - Laravel(沒有“Access-Control-Allow-Origin標頭 - Laravel)
Laravel Passport Route redirects to login page(Laravel Passport Route 重定向到登錄頁面)
主站蜘蛛池模板: 精品国产一区二区三区久久狼黑人 | 成人黄页在线观看 | 亚洲日本国产 | 日韩视频在线播放 | www.成人在线视频 | 国产精品伦一区二区三级视频 | 欧美成视频| 欧美成人激情视频 | 国产三级精品视频 | 久久青 | 欧美精品综合在线 | 国产精品69毛片高清亚洲 | 久久国产欧美日韩精品 | 老外几下就让我高潮了 | 久久99国产精品 | 成人一区二区三区 | 亚洲天堂一区 | 综合色站导航 | 久久久久久国产精品免费免费狐狸 | 精品一区av| 二区视频 | 欧美大片一区 | 欧美黑人体内she精在线观看 | 午夜免费av| 久热久热| 成人在线一区二区三区 | 欧美日韩国产高清视频 | 亚洲免费一区 | 男女羞羞视频免费看 | 国外成人在线视频 | 成人精品鲁一区一区二区 | 欧美日韩福利视频 | 日韩三级一区 | 伊人久久在线 | 欧美在线视频一区 | 久久婷婷色| 久久精品97 | 国产一区| 精品国偷自产在线 | 久久久免费在线观看 | 91精品国产91久久久久青草 |