Laravel created_at return object in place of date format in database?
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
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();
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');
});
}
...
}