-3

I'm working with Laravel Backpack and for a specific model I want the user to be able to add a "start time" using the time field type. I want the user to enter this format: hh:mm.

While creating the entity it works fine. But in the edit mode the value of the input field has this format: hh:mm:ss

The field is declared like this:

[
    'name'  => 'start_time',
    'label' => "Beginn",
    'type'  => 'time'
],

The corresponding field in the DB looks like this:

$table->time('start_time')->nullable();

When the model is created this works as expected what means, that I can enter the time in hh:mm format. For example I enter "14:00".

After saving the model, in the DB I have the value stored as 14:00:00.

Now, when I go to the edit view the value in the corresponding time input field is also 14:00:00.

I assume this is because in the DB the value is stored including the seconds.

Is it possible to store a time in the DB without the seconds? So just in the format hh:mm?

For more details I have some screenshots that show what I meant:

  1. On creation enter image description here enter image description here

  2. The value in DB after saving enter image description here

  3. The input after loading the edit view enter image description here

Alexander
  • 1
  • 1
  • Have you checked here? https://laravel.com/docs/9.x/eloquent-mutators#date-casting For solution you can define a cast in your model: `'start_time' => 'date:hh:mm'` – cengsemihsahin Jan 03 '23 at 18:21
  • @cengsemihsahin I tried your approach. After adding that type cast the input field is empty in the edit mode :/ The value stored in the DB remains the same. – Alexander Jan 03 '23 at 18:26
  • You want to pass partial time to a mysql time field? Is this what you are trying to do? – RG Servers Jan 03 '23 at 19:11
  • @RGServers Yeah...this might be it. Basically, I think if I can store the time as hh:mm in the db, without the seconds, this might be the solution. – Alexander Jan 03 '23 at 19:56

1 Answers1

0

I think you can make it work by defining the step attribute in the field https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/time#using_the_step_attribute

Cheers

Pedro X
  • 849
  • 5
  • 13