0

I am looking for ideas how to create and/or fill workday times in Laravel Blade. At this moment I end up by: hardcoded input with values

current code

<input type="date" name="date">
<li class="list-group-item">
<input type="checkbox" name="time[]"  value="6">6:00
<input type="checkbox" name="time[]"  value="6:20">6:20
</li>

How to improve this using loops?

My goal is to create array with values

$times=["1" => "2021-08-20 6:00"],["2" => "2021-08-20 6:20"]]
Aipo
  • 1,805
  • 3
  • 23
  • 48

2 Answers2

1

You could define a start and end time along with a duration for each appointment.

$start = Carbon\Carbon::createFromTime(7, 0, 0, 'Europe/London');
$end = Carbon\Carbon::createFromTime(19, 0, 0, 'Europe/London');
$appointment_duration = 20;

Then use a loop to generate the timeslots between the start and end.

$appointments = [];

for ($appointment = $start;
     $appointment <= $end;
     $appointment->addMinutes($appointment_duration))  {
    
    $appointments[] = $appointment;

}

Once you have your appointments you can pass them to your view.

Peppermintology
  • 9,343
  • 3
  • 27
  • 51
0

Try to write your code like this

<input type="date" name="date">
<li class="list-group-item">
<input type="checkbox" name="time[1][]"  value="6">6:00
<input type="checkbox" name="time[2][]"  value="6:20">6:20
</li>

you will get code like this in controller

"time" => [
    1 => "6",
    2 => "6:20"

]
ahmed taher
  • 25
  • 1
  • 1
  • 5