how can disable days of week?

Hey friends, i need disable a days of week that i don-t have in the array in my table, i try whit this code but is not working, do you have any idea about how can fix this? i have this in my table row availabledays ["Lunes"] ["Sabado","Lunes","Jueves","Miercoles"] ["Miercoles","Lunes","Viernes","Sabado","Martes","Jueves","Domingo"] This is my code:
3 Replies
TranceCode
TranceCodeOP5mo ago
Forms\Components\Select::make('common_area_id')
->label('Tipo de área')
->required()
->relationship('commonareas', 'nombre')
->reactive()
->afterStateUpdated(function ($state, callable $get, callable $set) {
$commonArea = CommonArea::find($state);
if($commonArea) {
$set('valor', $commonArea->valor);
$set('tramoreserva', "{$commonArea->rangefrom} - {$commonArea->rangeuntil}");
$set('availabledays', $commonArea->availabledays);
// Show available days
//dd($commonArea->availabledays);
} else {
$set('valor', '');
$set('tramoreserva', '');
$set('availabledays', []);
}
}),
Forms\Components\Hidden::make('availabledays')
->reactive(),
Forms\Components\DatePicker::make('fecha')
->label('Fecha')
->minDate(now()->toDateString()) // Fecha mínima de solicitud la actual o mayor
->required()
->reactive()
->afterStateUpdated(function ($state, callable $get, callable $set) {
$commonArea = CommonArea::find($get('common_area_id'));
if ($commonArea && !empty($commonArea->availabledays)) {
$set('availabledays', $commonArea->availabledays);
} else {
$set('availabledays', []);
}
})
->disabledDates(function (callable $get) {
$availableDays = $get('availabledays');
if (!$availableDays) {
return [];
}
// Mapeo de los días de la semana a sus índices correspondientes
$dayMap = [
'Domingo' => 0,
'Lunes' => 1,
'Martes' => 2,
'Miercoles' => 3,
'Jueves' => 4,
'Viernes' => 5,
'Sabado' => 6,
];
$allowedDays = array_map(fn($day) => $dayMap[$day] ?? null, $availableDays);
$allowedDays = array_filter($allowedDays, fn($day) => $day !== null);
$unavailableDays = array_diff([0,1,2,3,4,5,6], $allowedDays);
return function (\Carbon\Carbon $date) use ($unavailableDays) {
return in_array($date->dayOfWeek, $unavailableDays);
};
}),
Forms\Components\Select::make('common_area_id')
->label('Tipo de área')
->required()
->relationship('commonareas', 'nombre')
->reactive()
->afterStateUpdated(function ($state, callable $get, callable $set) {
$commonArea = CommonArea::find($state);
if($commonArea) {
$set('valor', $commonArea->valor);
$set('tramoreserva', "{$commonArea->rangefrom} - {$commonArea->rangeuntil}");
$set('availabledays', $commonArea->availabledays);
// Show available days
//dd($commonArea->availabledays);
} else {
$set('valor', '');
$set('tramoreserva', '');
$set('availabledays', []);
}
}),
Forms\Components\Hidden::make('availabledays')
->reactive(),
Forms\Components\DatePicker::make('fecha')
->label('Fecha')
->minDate(now()->toDateString()) // Fecha mínima de solicitud la actual o mayor
->required()
->reactive()
->afterStateUpdated(function ($state, callable $get, callable $set) {
$commonArea = CommonArea::find($get('common_area_id'));
if ($commonArea && !empty($commonArea->availabledays)) {
$set('availabledays', $commonArea->availabledays);
} else {
$set('availabledays', []);
}
})
->disabledDates(function (callable $get) {
$availableDays = $get('availabledays');
if (!$availableDays) {
return [];
}
// Mapeo de los días de la semana a sus índices correspondientes
$dayMap = [
'Domingo' => 0,
'Lunes' => 1,
'Martes' => 2,
'Miercoles' => 3,
'Jueves' => 4,
'Viernes' => 5,
'Sabado' => 6,
];
$allowedDays = array_map(fn($day) => $dayMap[$day] ?? null, $availableDays);
$allowedDays = array_filter($allowedDays, fn($day) => $day !== null);
$unavailableDays = array_diff([0,1,2,3,4,5,6], $allowedDays);
return function (\Carbon\Carbon $date) use ($unavailableDays) {
return in_array($date->dayOfWeek, $unavailableDays);
};
}),
So my plan is that if Monday exists in my array, it will only be shown as available to select the date Monday, the others that are disabled, if in another row I have Monday, Wednesday and Friday, that these 3 days are available, the others that are deactivated...
lazydog
lazydog5mo ago
Check this one, this code will disable Monday options by implementing
disableOptionWhen
disableOptionWhen
method
Forms\Components\Select::make('days')
->options(['mon' => 'Monday', 'tue' => 'Tuesday'])
->disableOptionWhen(function ($value) {
$toBeDisabled = ['mon'];
return in_array($value, $toBeDisabled);
}),
Forms\Components\Select::make('days')
->options(['mon' => 'Monday', 'tue' => 'Tuesday'])
->disableOptionWhen(function ($value) {
$toBeDisabled = ['mon'];
return in_array($value, $toBeDisabled);
}),
TranceCode
TranceCodeOP5mo ago
that it's not working bro, but thank you, i fix my problem whit this code!
Forms\Components\Select::make('common_area_id')
->label('Tipo de área')
->required()
->relationship('commonareas', 'nombre')
->reactive()
->afterStateUpdated(function ($state, callable $get, callable $set) use ($tenant) {
$commonArea = CommonArea::find($state);
if ($commonArea) {
$set('valor', $commonArea->valor);
$set('tramoreserva', "{$commonArea->rangefrom} - {$commonArea->rangeuntil}");
$set('availabledays', $commonArea->availabledays ?? []); // Asegura que sea un array

// Obtener reservas existentes para el área común seleccionada
$reservasExistentes = ReserveArea::where('common_area_id', $state)
->where('company_id', $tenant->id) // Filtrar por tenant logeado
->where('fecha', '>=', now()->toDateString()) // Solo reservas futuras
->pluck('fecha');

// Deshabilitar los días que ya están reservados
$set('disabledDays', $reservasExistentes->toArray());
} else {
$set('valor', '');
$set('tramoreserva', '');
$set('availabledays', []);
$set('disabledDays', []);
}
}),
Forms\Components\Select::make('common_area_id')
->label('Tipo de área')
->required()
->relationship('commonareas', 'nombre')
->reactive()
->afterStateUpdated(function ($state, callable $get, callable $set) use ($tenant) {
$commonArea = CommonArea::find($state);
if ($commonArea) {
$set('valor', $commonArea->valor);
$set('tramoreserva', "{$commonArea->rangefrom} - {$commonArea->rangeuntil}");
$set('availabledays', $commonArea->availabledays ?? []); // Asegura que sea un array

// Obtener reservas existentes para el área común seleccionada
$reservasExistentes = ReserveArea::where('common_area_id', $state)
->where('company_id', $tenant->id) // Filtrar por tenant logeado
->where('fecha', '>=', now()->toDateString()) // Solo reservas futuras
->pluck('fecha');

// Deshabilitar los días que ya están reservados
$set('disabledDays', $reservasExistentes->toArray());
} else {
$set('valor', '');
$set('tramoreserva', '');
$set('availabledays', []);
$set('disabledDays', []);
}
}),
Forms\Components\Hidden::make('availabledays')
->reactive(),
Forms\Components\DatePicker::make('fecha')
->label('Fecha')
->minDate(now()->toDateString()) // Fecha mínima de solicitud la actual o mayor
->required()
->reactive()
->native(false)
->firstDayOfWeek(1)
->disabledDates(function (callable $get) {
$availableDays = $get('availabledays') ?? []; // Asegura que sea un array
$reservedDates = $get('disabledDays') ?? []; // Asegura que sea un array
$disabledDates = [];
// Mapear días de la semana
$dayMap = [
'Lunes' => 1,
'Martes' => 2,
'Miercoles' => 3,
'Jueves' => 4,
'Viernes' => 5,
'Sabado' => 6,
'Domingo' => 7,
];
// Filtrar los días permitidos
$allowedDays = array_map(fn($day) => $dayMap[$day] ?? null, $availableDays);
$allowedDays = array_filter($allowedDays, fn($day) => $day !== null);
$unavailableDays = array_diff([0, 1, 2, 3, 4, 5, 6], $allowedDays);
// Generar las fechas deshabilitadas según días no permitidos
$start = Carbon::now();
$end = $start->copy()->addMonths(3); // Ejemplo de rango de 3 meses
while ($start->lessThanOrEqualTo($end)) {
if (in_array($start->dayOfWeek, $unavailableDays)) {
$disabledDates[] = $start->format('Y-m-d');
}
$start->addDay();
}
// Combinar con fechas reservadas
if ($reservedDates) {
$disabledDates = array_merge($disabledDates, $reservedDates);
}
return $disabledDates; // Devuelve un array combinado de fechas deshabilitadas
}),
Forms\Components\Hidden::make('availabledays')
->reactive(),
Forms\Components\DatePicker::make('fecha')
->label('Fecha')
->minDate(now()->toDateString()) // Fecha mínima de solicitud la actual o mayor
->required()
->reactive()
->native(false)
->firstDayOfWeek(1)
->disabledDates(function (callable $get) {
$availableDays = $get('availabledays') ?? []; // Asegura que sea un array
$reservedDates = $get('disabledDays') ?? []; // Asegura que sea un array
$disabledDates = [];
// Mapear días de la semana
$dayMap = [
'Lunes' => 1,
'Martes' => 2,
'Miercoles' => 3,
'Jueves' => 4,
'Viernes' => 5,
'Sabado' => 6,
'Domingo' => 7,
];
// Filtrar los días permitidos
$allowedDays = array_map(fn($day) => $dayMap[$day] ?? null, $availableDays);
$allowedDays = array_filter($allowedDays, fn($day) => $day !== null);
$unavailableDays = array_diff([0, 1, 2, 3, 4, 5, 6], $allowedDays);
// Generar las fechas deshabilitadas según días no permitidos
$start = Carbon::now();
$end = $start->copy()->addMonths(3); // Ejemplo de rango de 3 meses
while ($start->lessThanOrEqualTo($end)) {
if (in_array($start->dayOfWeek, $unavailableDays)) {
$disabledDates[] = $start->format('Y-m-d');
}
$start->addDay();
}
// Combinar con fechas reservadas
if ($reservedDates) {
$disabledDates = array_merge($disabledDates, $reservedDates);
}
return $disabledDates; // Devuelve un array combinado de fechas deshabilitadas
}),
Want results from more Discord servers?
Add your server