PHP code example of codewithkyrian / filament-date-range
1. Go to this page and download the library: Download codewithkyrian/filament-date-range library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
codewithkyrian / filament-date-range example snippets
use CodeWithKyrian\FilamentDateRange\Forms\Components\DateRangePicker;
DateRangePicker::make('event_period')
->label('Event Period'),
protected $casts = [
'event_period' => 'json',
];
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Support\Carbon;
protected function eventPeriod(): Attribute
{
return Attribute::make(
get: fn ($value, array $attributes) => [
'start' => $attributes['starts_at'] ?? null,
'end' => $attributes['ends_at'] ?? null,
],
set: fn (?array $value) => [
'starts_at' => $value['start'] ? Carbon::parse($value['start']) : null,
'ends_at' => $value['end'] ? Carbon::parse($value['end']) : null,
],
);
}
// To ensure the `event_period` virtual attribute is correctly handled during mass assignment operations (e.g., `Model::create([])` or `Model::update([])`),
// add its name to the `$fillable` array. This allows the `set` accessor to process the incoming date range.
protected $fillable = [
// other fillables
'event_period'
];
// To ensure your `event_period` attribute is available when Filament populates
// the form for an existing record, you may need to add it to the `$appends` array on your model:
protected $appends = [
'event_period',
];
use CodeWithKyrian\FilamentDateRange\Forms\Components\DateRangePicker;
DateRangePicker::make('event_period')
->label('Event Period')
DateRangePicker::make('booking_dates')
->displayFormat('d/m/Y') // Displays as "15/01/2024"
DateRangePicker::make('log_period')
->format('Y-m-d H:i:s') // Stores as "2024-01-15 10:30:00"