Set the Time Array to Option Select

I want to set the timeslots to the Select Options based on the appointment_date and appointment_time columns if match then hide that time
i try to debug the time and it works but it wasn't applying on the Select options.
image.png
image.png
image.png
Solution
Not sure if that's the same example but we had a similar project with radio timeslots in one of our tutorials, here's the code

public static function form(Form $form): Form
{
    $dateFormat = 'Y-m-d';
 
    return $form
        ->schema([
            DatePicker::make('date')
                ->native(false)
                ->minDate(now()->format($dateFormat))
                ->maxDate(now()->addWeeks(2)->format($dateFormat))
                ->format($dateFormat)
                ->required()
                ->live(),
            Radio::make('track')
                ->options(fn (Get $get) => self::getAvailableReservations($get))
                ->hidden(fn (Get $get) => ! $get('date'))
                ->required()
                ->columnSpan(2),
        ]);
}
 
public static function getAvailableReservations(Get $get): array
{
    $date                  = Carbon::parse($get('date'));
    $startPeriod           = $date->copy()->hour(14);
    $endPeriod             = $date->copy()->hour(16);
    $times                 = CarbonPeriod::create($startPeriod, '1 hour', $endPeriod);
    $availableReservations = [];
 
    $tracks = Track::with([
        'reservations' => function ($q) use ($startPeriod, $endPeriod) {
            $q->whereBetween('start_time', [$startPeriod, $endPeriod]);
        },
    ])
        ->get();
 
    foreach ($tracks as $track) {
        $reservations = $track->reservations->pluck('start_time')->toArray();
 
        $availableTimes = $times->copy()->filter(function ($time) use ($reservations) {
            return ! in_array($time, $reservations) && ! $time->isPast();
        })->toArray();
 
        foreach ($availableTimes as $time) {
            $key                         = $track->id . '-' . $time->format('H');
            $availableReservations[$key] = $track->title . ' ' . $time->format('H:i');
        }
    }
 
    return $availableReservations;
}
Was this page helpful?