Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.6] Ability to cast model attributes to a specific date format #22989

Merged
merged 2 commits into from
Feb 2, 2018

Conversation

themsaid
Copy link
Member

@themsaid themsaid commented Feb 1, 2018

Given the following casts:

public $casts = [
    'birthdate' => 'date:Y-m-d',
    'joined_at' => 'datetime:Y-m-d H:00',
];

When the model is casted to JSON or array output the attributes will be formatted to the date formats provided by the developer:

birthdate: 2000-10-15
joined_at: 2017-10-15 05:00

@sisve
Copy link
Contributor

sisve commented Feb 1, 2018

It's unclear from the example, does this also support datetime:Y-m-d (using the datetime rule instead of the date rule)?

@themsaid
Copy link
Member Author

themsaid commented Feb 1, 2018

No, date:* is a different casting type.

@sisve
Copy link
Contributor

sisve commented Feb 1, 2018

Is there any particular reason for this? Wouldn't it make sense to support formats on both date and datetimes? (And sure, you can write formats for date that includes times, and formats for datetimes that exclude times. That's just something we'll have to live with.)

@themsaid
Copy link
Member Author

themsaid commented Feb 1, 2018

you can write formats for date that includes times, and formats for datetimes that exclude times. That's just something we'll have to live with

That's the point, use date:.... with whatever format you wish.

@sisve
Copy link
Contributor

sisve commented Feb 1, 2018

I understand that, but there will be people surprised that their format is not working. Why should this work with date, and not with datetime? Can we implement it for both? Is there some limitation in the framework, like is the syntax already taken by something else? Or are we just attempting to surprise developers with "yeah, you can format date as datetimes, but datetimes themselves cannot be formatted"?

@themsaid
Copy link
Member Author

themsaid commented Feb 1, 2018

I'm not opposing having datetime: as alias for date:, let's wait and see what Taylor thinks.

@bkilshaw
Copy link

bkilshaw commented Feb 1, 2018

@themsaid Do you agree that having inconsistent behaviour is a bad thing? If so, why not update the PR to make sure both date and datetime are consistent with one another?

@taylorotwell
Copy link
Member

I'm fine with having datetime cast be customizable too.

@themsaid
Copy link
Member Author

themsaid commented Feb 1, 2018

Updated, now date:Y-m-d H:i and datetime:Y-m-d H:i would work

@taylorotwell taylorotwell merged commit f8abc3f into laravel:5.6 Feb 2, 2018
@monaye
Copy link
Contributor

monaye commented May 14, 2018

what is the best way to set format dynamically, based on user preference?

@BrandonSurowiec
Copy link
Contributor

BrandonSurowiec commented May 14, 2018

@monaye You could add a method to your Model that manipulates the internal $casts = []; array. (If for a JSON response).

PS. This repo isn't for support. Try asking your question on one of the many great community support areas that will likely give you a better answer more quickly:

@SharmaPrakhar25
Copy link

Sir, with all due respect I have tried to implement this property in my Laravel app which was successful in my local but I am unable to implement it on my server, is there anything we need to do to make this accomplish.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants