By default, the timestamps are cast as Carbon instances on the Eloquent Model, so the string stored in the database is parsed as a Carbon instance when you use the Eloquent Builder - because the results all are Libro model instances:
$libros = Libro::all();
The Carbon instance is cast / serialized as a 2021-03-24T13:04:57.000000Z because it
When you drop down to the Query Builder DB::table('libros') you get none of the casting, relationships, behaviours that Eloquent provides - you get a stdClass object with raw (string) data.
I will like to always get the date formated like this 2021-03-24 08:13:38
You can format a Carbon instance however you please using the underlying date formatters:
@foreach($libros as $libro)
{{ $libro->created_at->toDateTimeString() }}
@endforeach
Heres the code in case someone else is in the same situation
public function getCreatedAtAttribute($date)
{
return Carbon::parse($date)->timezone(date_default_timezone_get())->format('Y-m-d H:i:s');
}
public function getUpdatedAtAttribute($date)
{
return Carbon::parse($date)->timezone(date_default_timezone_get())->format('Y-m-d H:i:s');
}