Laravel created_at return object in place of date format in database? [closed]
Asked Answered
E

4

15

I am trying to return created_at datetime from users table in JSON response in Laravel.

In my databaes it show the value as 2016-07-18 00:00:00 but when I try to return in JSON api it converts into

{
    date: "2016-07-18 00:00:00.000000",
    timezone_type: 3,
    timezone: "UTC"
}

How can I fix this problem?

Eller answered 26/12, 2017 at 10:46 Comment(1)
Please share your codeSedimentation
R
14

You have Carbon in Laravel framework that helps you to make your datetime as you want with below code.

        $created_at = new Carbon($value)->toDateTimeString();

now pass $created_at at place of created_at. example code for users table (NOTE: not tested please check yourself that you need to pass an object or array)

        $user = User::find(1);
        $user->created_at = new Carbon($user->created_at)->toDateTimeString();
Redstone answered 26/12, 2017 at 10:50 Comment(1)
Another easy way is by adding following code to your model, so that when you want json or array, you'll get unix timestamp, otherwise you can still have Carbon data type for all the date fields within $dates. protected function serializeDate(\DateTimeInterface $date) { return $date->format('U'); }Alphabetic
F
24

By default created_at and updated_at are Carbon objects, so you can do just:

$object->created_at->toDateTimeString();

to get again in format Y-m-d H:i:s

Foetus answered 26/12, 2017 at 10:53 Comment(1)
Your saved my life, tnxHoagy
R
14

You have Carbon in Laravel framework that helps you to make your datetime as you want with below code.

        $created_at = new Carbon($value)->toDateTimeString();

now pass $created_at at place of created_at. example code for users table (NOTE: not tested please check yourself that you need to pass an object or array)

        $user = User::find(1);
        $user->created_at = new Carbon($user->created_at)->toDateTimeString();
Redstone answered 26/12, 2017 at 10:50 Comment(1)
Another easy way is by adding following code to your model, so that when you want json or array, you'll get unix timestamp, otherwise you can still have Carbon data type for all the date fields within $dates. protected function serializeDate(\DateTimeInterface $date) { return $date->format('U'); }Alphabetic
P
6

You can change that behavior by adding serializeUsing method In your AppServiceProvider class to update the json serialization format of dates, it's only affect api calls (docs), here is an example:

use Illuminate\Support\Carbon;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Carbon::serializeUsing(function ($carbon) {
            return $carbon->format('Y-m-d H:i:s');
        });
    }

    ...

}
Paternal answered 26/12, 2017 at 10:59 Comment(1)
I have added this code to my Application and it does not seem to be working... Any ideas?Sycophancy
G
4

Since you want to get JSON response, you can use Eloquent API Resource to transform Carbon object to any format, for example:

return [
    'created_at' => $this->created_at->toDateTimeString(),
    ....
];
Gaylene answered 26/12, 2017 at 11:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.