laraning/nova-time-field

It Doesn't Work With Date Mutators

Closed this issue · 2 comments

This field works perfectly fine on nova and the front end, but I added a date mutator to the model so that I could format the date:

public function getStartTimeAttribute($value) { return \Carbon\Carbon::parse($value)->format('g:ia'); }

This works flawlessly on the front end but when trying to go back into Nova it gives an error:

A two digit second could not be found

So I tried:

TimeField::make('Start Time', function(){ return Carbon::parse($this->start_time)->format('g:i'); }),

And it sows on the detail and list views but the fields are not shown on the edit page.

Any ideas?

Maybe adding ->format() as a method to the field would resolve this issue.

@DerekBuntin

I had to do two things:

One in the Model:

/**
 * Creates the $this->start_time attribute for the Nova framework.
 *
 * @return \Carbon\Carbon|null
 */
public function getStartTimeAttribute()
{
    return !is_null($this->starts_at) ? carbon($this->starts_at)->format('H:i:s') : null;
}

And another in the resource:

TimeField::make('Start Time', 'start_time', function($value, $resource) {
    return Carbon::createFromFormat('H:i:s', $value)->format('g:i A');
})->withTwelveHourTime(),

This makes the Index, Detail, and Create pages work appropriately. I'm still trying to sort out how to make this play nice on the Update page (for the 12 hour format), as the AM / PM information gets lost.

Closed with the PR