datomatic / filament-db-opening-hours
Filament plugin to manage opening hours stored with datomatic/db-opening-hours
Package info
github.com/datomatic/filament-db-opening-hours
pkg:composer/datomatic/filament-db-opening-hours
Fund package maintenance!
Requires
- php: ^8.2
- datomatic/db-opening-hours: ^1.1
- filament/filament: ^5.0
- illuminate/contracts: ^12.0|^13.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.18
- nunomaduro/collision: ^8.0
- orchestra/testbench: ^10.0|^11.0
- pestphp/pest: ^3.0|^4.0|^5.0
- pestphp/pest-plugin-arch: ^3.0|^4.0|^5.0
- pestphp/pest-plugin-laravel: ^3.0|^4.0|^5.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A Filament plugin to create and edit opening hours stored with datomatic/db-opening-hours: a weekly schedule with multiple time ranges per day, plus date exceptions (holidays, special openings).
It adds an Opening hours resource to your panel with one tab per weekday and a tab for the exceptions.
Requirements
| Package | Version |
|---|---|
| PHP | 8.2 – 8.6 |
| Laravel | 12, 13 |
| Filament | 5 |
datomatic/db-opening-hours |
^1.1 |
Installation
Install the package via composer:
composer require datomatic/filament-db-opening-hours
Run the migrations of datomatic/db-opening-hours (they are loaded automatically):
php artisan migrate
Optionally publish the config file:
php artisan vendor:publish --tag="filament-db-opening-hours-config"
Optionally publish the translations (English, Italian and Dutch are included):
php artisan vendor:publish --tag="filament-db-opening-hours-translations"
Usage
Register the plugin on your panel:
use Datomatic\FilamentDatabaseOpeningHours\FilamentDatabaseOpeningHoursPlugin; public function panel(Panel $panel): Panel { return $panel // ... ->plugin(FilamentDatabaseOpeningHoursPlugin::make()); }
Navigation
use Filament\Support\Icons\Heroicon; FilamentDatabaseOpeningHoursPlugin::make() ->navigationGroup('Settings') ->navigationIcon(Heroicon::OutlinedClock) ->navigationSort(10);
Custom resource
Extend the resource to change anything (authorization, extra columns, a different model…) and register your class instead:
use Datomatic\FilamentDatabaseOpeningHours\Resources\OpeningHourResource; class MyOpeningHourResource extends OpeningHourResource { // ... } FilamentDatabaseOpeningHoursPlugin::make() ->resource(MyOpeningHourResource::class);
The resource uses the model configured in db-opening-hours.models.opening_hour, so a model swapped through the db-opening-hours config is picked up automatically.
Embedding the schedule in another form
The resource is optional. To edit a schedule inside another form, for example in a section of a model's edit page, use OpeningHourForm::scheduleTabs(). It returns the weekday and exception tabs without the "General" tab. Set a statePath() on the parent component, then load and save the state with OpeningHourSchedule (see below):
use Datomatic\FilamentDatabaseOpeningHours\Resources\OpeningHourResource\Schemas\OpeningHourForm; use Filament\Schemas\Components\Section; Section::make('Opening hours') ->statePath('opening_hours') ->schema([ OpeningHourForm::scheduleTabs('opening-hours'), ]);
// In the edit page of the model protected function mutateFormDataBeforeFill(array $data): array { $data['opening_hours'] = app(OpeningHourSchedule::class)->toFormState($this->getRecord()->openingHour); return $data; } // The schedule is not a column of the model protected function mutateFormDataBeforeSave(array $data): array { unset($data['opening_hours']); return $data; } protected function afterSave(): void { app(OpeningHourSchedule::class)->update($this->getRecord()->openingHour, $this->data['opening_hours']); }
Use a different id for each schedule on the same page: the id is also the query string key that keeps the active tab. If you only embed the schedule, you do not need to register the plugin on the panel.
Saving the schedule from your own code
The form is persisted by Datomatic\FilamentDatabaseOpeningHours\Support\OpeningHourSchedule, which you can also use directly (for example from a custom page or an action). Every write runs in a database transaction, and removing a weekday or an exception also removes its time ranges.
use Datomatic\FilamentDatabaseOpeningHours\Support\OpeningHourSchedule; $schedule = app(OpeningHourSchedule::class); $openingHour = $schedule->create([ 'name' => 'Main shop', 'days' => [ 'monday' => [ 'time_ranges' => [ ['start' => '09:00', 'end' => '13:00'], ['start' => '14:00', 'end' => '18:00'], ], ], ], 'exceptions' => [ ['date' => '2026-12-25', 'description' => 'Christmas', 'time_ranges' => []], ], ]); // Only the keys you pass are synced: this replaces the exceptions and keeps the weekly days. $schedule->update($openingHour, ['exceptions' => []]); $schedule->toFormState($openingHour); // the same shape, as used by the form $schedule->delete($openingHour); // removes days, exceptions and time ranges too
Notes:
- A weekday is stored only when it has at least one time range or a description.
- An exception without time ranges is kept: it marks that date as closed.
- Exceptions are matched by date, so the same date cannot be added twice.
Config
use Datomatic\DatabaseOpeningHours\Enums\Day; return [ // Show the "General" tab with a (required) name field, useful when a model holds more than one schedule 'general_description' => false, // Show a description field for each weekday 'day_description' => false, // Show a description field for each time range 'time_range_description' => false, // Show a description field for each date exception 'exception_description' => false, // The weekday shown as the first tab of the form 'first_day_of_week' => Day::MONDAY, ];
Upgrading from 0.x
Version 1 requires Filament 5 and datomatic/db-opening-hours ^1.1.
- The resource and its pages are no longer
final, so they can be extended. - Days and exceptions are saved through
OpeningHourScheduleinstead of Filament relationship components: deleting a day, an exception or a whole schedule no longer leaves orphaned time ranges behind. - The Tailwind/PostCSS configuration files were removed: the plugin ships no custom views.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.