I'm making a table in Laravel and I want my "besttime" to be a nullable
but it's returning the current time if I keep it empty.
(I'm using carbon because I want the H:i
format without the seconds)
here's what my input looks like
@foreach ($records as $record)
<tr>
<td>{{ Carbon\Carbon::parse($record->besttime)->format('H:i') }}</td>
</tr>
@endforeach
I will suggest you to add mutators in model so you can keep code simpler in blade file
public function getBesttimeAttribute($value)
{
return empty($value)
? null
: Carbon::parse($value)->format('H:i');
}
so in your view you can do
<td>{{ $record->besttime }}</td>
suppose if you dont want to modify orginal values then you can create custom attribute and append it to request
public function getCustomBestTimeAttribute()
{
return empty($this->besttime)
? null
: Carbon::parse($this->besttime)->format('H:i');
}
and set
public $appends = ["custom_best_time"];
so in your view you can do
<td>{{ $record->custom_best_time}}</td>