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.
No description
No description
No description
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 {...
Jump to solution
4 Replies
IamJohnDev
IamJohnDev2mo ago
No description
Solution
Povilas K
Povilas K2mo ago
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;
}
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;
}
Povilas K
Povilas K2mo ago
You can maybe compare output format or logic
IamJohnDev
IamJohnDev2mo ago
Thank you so much, Sir Povilas, for this code snippet from your tutorial. I forgot that you have a tutorial with the same implementation that I need, even though I watched it a few months ago.