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
Thats not a filament issue, you can generate dates with the carbon package, like Carbon::now()->addWeeks(1) or something like that
Carbon - A simple PHP API extension for DateTime.
Carbon - A simple PHP API extension for DateTime.
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
can you share you code snippet first?
it wont be that hard to integrate it with the table query
Filament Daily
YouTube
Filament: Get Available Time Slots with Date Picker
An example of DatePicker and Radio with dynamic options.
Code is here: https://filamentexamples.com/project/timeslots-booking-by-date
Thats up to you to decide. You can have a button action to generate the dates or a db seeder.
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(),
]);
}
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
Laravel Daily
Filament Date Picker: Disable Dates with Conditions
If you don't want the date to disappear remove the condition in here
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.
I want to hide the hours that are already registered for a given day
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...
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.
Yes and that dont work that hour is still present
I am confused, you said the hour is still present but it also disappears after it is booked?
well are you persisting the data so that the service can retrieve the correct dates?
Data is in database
if you do dd($availableTimes) do you get the correct amount of available times?
I'll check when I get home
on tabel i see this

in form i got this

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.
this $availableTimes = $times->filter(function ($time) use ($reservations) {
return ! in_array($time, $reservations);
})->toArray();?
dump($times);
dd($reservations);
What do you see.

Ok try
it work
Can you explain to me what I had wrong?
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.
thank you your help means a lot to me
Welcome buddy
Cleaner version
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(),

because the value from date is not a date formate.