How to Format Date in Vuejs Laravel without Moment.js
When you are working with Laravel and Vue js and you want to display the created_at date time you need to format the value to be human-readable, sometimes you prefer to use a library like moment.js, but today we are going to see how we can do that without moment.js.
Use laravel accessor to format the created_at attribute
So to format the created_at without Moment.js we use Laravel accessor which is called automatically when attempting to retrieve the value of the created_at attribute, once done you can display created_at human-readable in your Vue js component.
<?php
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
public function getCreatedAtAttribute($value){
return Carbon::parse($value)->diffForHumans();
}
}