Mutators and Accessors in Laravel
Set file path in model
public function getFilePathAttribute($file_path)
{
if(!empty($file_path))
{
return $filepath = URL::to('/') . '/upload/' . $file_path;
/* return asset('/upoad/' . $file_path); */
}
}
Set created time ago in model
protected $appends = ["created_time_ago"];
public function getCreatedTimeAgoAttribute()
{
$created_at = $this->attributes['created_at'];
// Convert the date/time to a Carbon instance
$carbonCreatedAt = Carbon::parse($created_at);
// Get the "time ago" format
$timeAgo = $carbonCreatedAt->diffForHumans();
// Set the modified value to the column
return $timeAgo;
}
Set created time ago in model
public function getRequestedTimeAttribute($requested_time)
{
if(!empty($requested_time))
{
return date_format($requested_time,"Y-m-d");
}
}
Comments
Post a Comment