Is it possible to generate future dates in Filament

I want to create a booking system that shows available appointment slots, for example, showing a free slot in two weeks, and displays dates from today up to the two-week period
32 Replies
Bruno Pereira
Bruno Pereira3w ago
Thats not a filament issue, you can generate dates with the carbon package, like Carbon::now()->addWeeks(1) or something like that
jajla
jajlaOP3w ago
but how to use it in filament later? because in the model I made a loop that iterated over the date from today to, for example, the date in 7 days and it called these days but I don't know how to use it in the table in filament
Mohamed Ayaou
Mohamed Ayaou3w ago
can you share you code snippet first? it wont be that hard to integrate it with the table query
Bruno Pereira
Bruno Pereira3w ago
Thats up to you to decide. You can have a button action to generate the dates or a db seeder.
jajla
jajlaOP3w ago
Thanks I tried the method from the link but it doesn't work for me, the hour disappears after it is already booked I make UserVisitService this is the code public function getAvailableTimesForDate(string $date): array { $date = Carbon::parse($date); $startPeriod = $date->copy()->hour(9); $endPeriod = $date->copy()->hour(18); $times = CarbonPeriod::create($startPeriod, '30 minutes', $endPeriod); $availableReservations = []; $reservations = Visit::whereDate('date', $date) ->pluck('time') ->toArray(); $availableTimes = $times->filter(function ($time) use ($reservations) { return ! in_array($time, $reservations); })->toArray(); foreach ($availableTimes as $time) { $key = $time->format('H:i'); $availableReservations[$key] = $time->format('H:i'); } return $availableReservations; } And UserVisitResource public static function form(Form $form): Form { $dateFormat = 'Y-m-d'; return $form ->schema([ DatePicker::make('date') ->minDate(now()->format($dateFormat)) ->required() ->live(), Radio::make('time') ->options(fn(Get $get) => (new UserVisitService())->getAvailableTimesForDate($get('date'))) ->hidden(fn(Get $get) => !$get('date')) ->required() ->columnSpan(2), Select::make('service_type') ->label(__('trans.form.service_type')) ->options( collect(Services::cases()) ->mapWithKeys(fn($role) => [$role->value => $role->getLabel()]) ->toArray() )->required(), ]); }
toeknee
toeknee3w ago
I think the problem there is because it's no longer available. so you want to add in disabledDates for booked dates/times ? https://laraveldaily.com/post/filament-date-picker-disable-dates-with-conditions
Bruno Pereira
Bruno Pereira3w ago
If you don't want the date to disappear remove the condition in here
$availableTimes = $times->filter(function ($time) use ($reservations) {
return ! in_array($time, $reservations);
})->toArray();
$availableTimes = $times->filter(function ($time) use ($reservations) {
return ! in_array($time, $reservations);
})->toArray();
This or you didn't explain correctly what you want to achieve. Because the Povilas video makes sense to remove the date when isn't available.
jajla
jajlaOP3w ago
I want to hide the hours that are already registered for a given day
Bruno Pereira
Bruno Pereira3w ago
Are you persisting that information in the database? just saw the video and it does that, he picked the day 28 and choose a time, then another user also picked the day 28 and the hour previous picked isnt in the list...
toeknee
toeknee3w ago
But you said: I tried the method from the link but it doesn't work for me, the hour disappears after it is already booked [22:19] So that's what you asked for.
jajla
jajlaOP3w ago
Yes and that dont work that hour is still present
toeknee
toeknee3w ago
I am confused, you said the hour is still present but it also disappears after it is booked?
Bruno Pereira
Bruno Pereira3w ago
well are you persisting the data so that the service can retrieve the correct dates?
jajla
jajlaOP3w ago
Data is in database
Bruno Pereira
Bruno Pereira3w ago
if you do dd($availableTimes) do you get the correct amount of available times?
jajla
jajlaOP3w ago
I'll check when I get home
jajla
jajlaOP3w ago
on tabel i see this
No description
jajla
jajlaOP3w ago
in form i got this
No description
toeknee
toeknee3w ago
So you need to check return ! in_array($time, $reservations); I would dd the reservations and ensure they are scoped to the time. You may find time is different to the key'd time slightly.
jajla
jajlaOP3w ago
this $availableTimes = $times->filter(function ($time) use ($reservations) { return ! in_array($time, $reservations); })->toArray();?
toeknee
toeknee3w ago
dump($times); dd($reservations); What do you see.
jajla
jajlaOP3w ago
No description
toeknee
toeknee3w ago
Ok try
public function getAvailableTimesForDate(string $date): array
{
$date = Carbon::parse($date);
$startPeriod = $date->copy()->hour(9);
$endPeriod = $date->copy()->hour(18);
$times = CarbonPeriod::create($startPeriod, '30 minutes', $endPeriod);
$availableReservations = [];

$reservations = Visit::whereDate('date', $date)
->pluck('time')
->map(function ($time) {
return Carbon::parse($time)->format('H:i');
})
->toArray();

$availableTimes = $times->filter(function ($time) use ($reservations) {
return ! in_array($time->format('H:i'), $reservations);
})->toArray();

foreach ($availableTimes as $time) {
$key = $time->format('H:i');
$availableReservations[$key] = $time->format('H:i');
}

return $availableReservations;
}
public function getAvailableTimesForDate(string $date): array
{
$date = Carbon::parse($date);
$startPeriod = $date->copy()->hour(9);
$endPeriod = $date->copy()->hour(18);
$times = CarbonPeriod::create($startPeriod, '30 minutes', $endPeriod);
$availableReservations = [];

$reservations = Visit::whereDate('date', $date)
->pluck('time')
->map(function ($time) {
return Carbon::parse($time)->format('H:i');
})
->toArray();

$availableTimes = $times->filter(function ($time) use ($reservations) {
return ! in_array($time->format('H:i'), $reservations);
})->toArray();

foreach ($availableTimes as $time) {
$key = $time->format('H:i');
$availableReservations[$key] = $time->format('H:i');
}

return $availableReservations;
}
jajla
jajlaOP3w ago
it work Can you explain to me what I had wrong?
toeknee
toeknee3w ago
It's the mapping on the reservations You are getting times with carbon, but you just pulled the value from the db for the reservations so when you check if in array it was always false as they didn't match. Maping them in a carbon way works as that.
jajla
jajlaOP3w ago
thank you your help means a lot to me
toeknee
toeknee3w ago
Welcome buddy Cleaner version
public function getAvailableTimesForDate(string $date): array
{
$date = Carbon::parse($date);
$startPeriod = $date->copy()->hour(9);
$endPeriod = $date->copy()->hour(18);
$times = CarbonPeriod::create($startPeriod, '30 minutes', $endPeriod);

$reservations = Visit::whereDate('date', $date)
->pluck('time')
->map(fn($time) => Carbon::parse($time)->format('H:i'))
->toArray();

$availableReservations = [];
foreach ($times as $time) {
$formattedTime = $time->format('H:i');
if (!in_array($formattedTime, $reservations)) {
$availableReservations[$formattedTime] = $formattedTime;
}
}

return $availableReservations;
}
public function getAvailableTimesForDate(string $date): array
{
$date = Carbon::parse($date);
$startPeriod = $date->copy()->hour(9);
$endPeriod = $date->copy()->hour(18);
$times = CarbonPeriod::create($startPeriod, '30 minutes', $endPeriod);

$reservations = Visit::whereDate('date', $date)
->pluck('time')
->map(fn($time) => Carbon::parse($time)->format('H:i'))
->toArray();

$availableReservations = [];
foreach ($times as $time) {
$formattedTime = $time->format('H:i');
if (!in_array($formattedTime, $reservations)) {
$availableReservations[$formattedTime] = $formattedTime;
}
}

return $availableReservations;
}
jajla
jajlaOP3w ago
thanks for me its magic 🤣 DatePicker::make('date') ->minDate(now()->format($dateFormat)) //->native(false) ->required() ->formatStateUsing(fn($state) => Carbon::parse($state) ->translatedFormat('j F l')) ->live(),
jajla
jajlaOP3w ago
No description
toeknee
toeknee3w ago
because the value from date is not a date formate.

Did you find this page helpful?