I got the solution that you can use anywhere with casting attribute. Check Code below
<?php
namespace App\Casts;
use Carbon\Carbon;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
class CreateAtCast implements CastsAttributes
{
/**
* Transform the attribute from the underlying model values.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
* @return mixed
*/
public function get($model, string $key, $value, array $attributes)
{
$date = Carbon::parse($value)->diffForHumans();
return $date;
}
/**
* Transform the attribute to its underlying model values.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
* @return array
*/
public function set($model, string $key, $value, array $attributes)
{
return $value;
}
}
in Modal
<?php
namespace App;
use App\Casts\CreateAtCast;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $casts = [
'email_verified_at' => 'datetime',
'created_at' => CreateAtCast::class,
];
protected $dates = ['created_at', 'updated_at'];
}
in vue template just use - user.create_at