3

i have time column in my data base . i need to defined field for this column.

this is my migration:

 $table->date('date');
            $table->time('time_begin');
            $table->time('time_Enable');

this is my resource:

    public function fields(Request $request)
    {
        return [

            datetime::make('time_Enable'),
            datetime::make('time_begin'),

        ];
    }

i just need time not date and time

3 Answers3

2

Impressively enough, Nova not only doesn't have a Time field but also doesn't support a time-only usage of DateTime field.

On the other side, Nova has plenty of third parties componentes that can be easily added via composer. I did look for some on Nova Packages and found two viable options: Laraning Time Field and Michielfb Time Field, they seem to be pretty good and open-source.

Nonetheless, they have relatively few stars on GitHub and a rather old latest version, which is not much of a problem for such a simples functionality, but I'd rather go with a native one, so here is how I implemented it:

Text::make('Duration')
    ->placeholder('##:##:##')
    ->rules('date_format:"H:i:s"')
    ->help('hh:mm:ss'),

So basically I used the Text field with a format validation. Also added the placeholder and help attributes to make it a bit more intuitive. It works well! and I'm using it with a time database field. Other colleges suggested using a masked component but it would also require a third party one and I didn't find a good option.

Eduardo Pacheco
  • 504
  • 4
  • 14
1

You can use the extraAttributes option with type key:

Text::make('time_begin')->withMeta(['extraAttributes' => ['type' => 'time']])
Javlon Tulkinov
  • 560
  • 1
  • 5
  • 21
0

To work with time in nova 4, I did the following what is working fine for now.

DateTime::make('time_to')->displayUsing(function ($value) {
    return $value->format('H:i');
})->exceptOnForms(),

Text::make('time_to')->resolveUsing(function ($date) {
    $timestamp = new \DateTime($date);
    return $timestamp->format('H:i');
})->rules('date_format:"H:i"')->withMeta(['extraAttributes' => ['type' => 'time']])->onlyOnForms(),