Hello I have laravel project that store data created_at as a timestamp, I would call it from database, but it still show timestamp format, how can I convert it to date format without changing my column type? image
here is my model:
public function getCreatedAtAttribute($value)
{
return Carbon::parse($value)->timestamp;
}
//fungsi untuk merubah format tanggal diubah ke timestamp
public function getUpdatedAtAttribute($value)
{
return Carbon::parse($value)->timestamp;
}
and how should I call it in view or .blade file??
Thank you
4
Answers
To convert to human readable string try changing your functions as below:
toFormattedDateString()
function will convert to format likeJun 22, 2020
. See this article for more alternatives https://www.leonelngande.com/format-dates-for-humans-with-carbon-in-php/.To use in blade file simply access the parameter like:
In your model:
For use:
If you pass the data from controller to blade You can call in your blade file like this …
Or
if you want to convert to inside controller.
fungsi untuk merubah format tanggal diubah ke timestamp
Or you can try carbon
fungsi untuk merubah format tanggal diubah ke timestamp
I would suggest to use eloquent’s attribute casting instead.
So for example, make
created_at
shown as date ind-m-Y
format:Note that attribute casting will only effects the attibute when the model is serialized to json or array (as stated in the doc).