How do I change the date format Laravel outputs to JSON?

后端 未结 4 1402
南方客
南方客 2020-12-17 19:24

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

4条回答
  •  既然无缘
    2020-12-17 20:23

    I strongly suggest you use the Carbon class to handle all your dates and datetimes variables, it already comes with Laravel 5 so you can start using whenever you want.

    Check it out on Carbon Repo to see what you can do with it.

    As an example, you can format dates from your model like this

    Carbon::parse($model->created_at)->format('d-m-Y')
    

    As for a good approach, I would suggest to use the Repository Pattern along with Presenters and Transformers. By using it you can define how you want your json to be displayed/mounted and opt to skip the presenter whenever you want in order to still get you Eloquent model returned when you make your queries.

提交回复
热议问题