如何将Laravel输出的日期格式更改为JSON? [英] How do I change the date format Laravel outputs to JSON?

查看:405
本文介绍了如何将Laravel输出的日期格式更改为JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Laravel中构建了一个应用程序,并且雄辩的返回日期的格式为:2015-04-17 00:00:00.我正在向JSON发送一个特定的查询,以便可以使用D3制作图形,我想我希望使用ISO8601('1995-12-17T03:24:00')或其他与javascript Date()构造函数配合使用的其他格式的日期.

是否有办法将Laravel端上输出的日期格式更改为JSON?我不确定使用mutator是最好的方法,因为它会影响应用程序其他部分的日期.

还是将JSON输出保持不变,并使用一些JavaScript字符串方法在将日期格式传递给Date()构造函数之前对日期格式进行操作会更好?哪种方法更有效?

这是我的模特:

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:

[{"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()

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

或者我自己选择创建特征

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

然后添加

class SomeClassWithDates extends Model {    
    use UnixTimestampSerializable;

    ...
}

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.

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.

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?

Here is my model:

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();
}
}

Here is my query:

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

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

解决方案

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();
    }
}

and then add

class SomeClassWithDates extends Model {    
    use UnixTimestampSerializable;

    ...
}

这篇关于如何将Laravel输出的日期格式更改为JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆