roundly-consulting / options-for-laravel
Manage global or per-entity options and preferences with typed casts and in-request caching.
Package info
github.com/roundly-consulting/options-for-laravel
pkg:composer/roundly-consulting/options-for-laravel
Fund package maintenance!
Requires
- php: ^8.4
- illuminate/contracts: ^12.0|^13.0
- roundly-consulting/package-toolkit-for-laravel: ^1.0
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.18
- nunomaduro/collision: ^8.0
- orchestra/testbench: ^10.0|^11.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- roundly-consulting/testing-for-laravel: ^1.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Options for Laravel
Global or per-model options, settings and preferences for Laravel. Each option is a small class with its key, default value, cast and validation rules; values can belong to the whole app or to any Eloquent model (a user, a team, a tenant) and are cached, typed and evented.
Installation
Requires PHP 8.4 and Laravel 12 or 13.
composer require roundly-consulting/options-for-laravel
php artisan vendor:publish --tag="options-migrations"
php artisan migrate
If your owner models have UUID/ULID keys, set OPTIONS_KEY_TYPE before migrating.
Usage
Describe an option once (php artisan make:option ThemeOption --key=theme scaffolds it):
use RoundlyConsulting\Options\BaseOption; final class ThemeOption extends BaseOption { public function key(): string { return 'theme'; } public function default(): mixed { return 'light'; } public function rules(): array|string { return ['in:light,dark']; } }
Then read and write it — globally or for any model:
use RoundlyConsulting\Options\Facades\Options; Options::get(ThemeOption::class); // 'light' — the default until a value is stored Options::set(ThemeOption::class, 'dark'); // validated against rules(), cached, fires OptionSet Options::set(ThemeOption::class, 'light', $user); // scoped to one model Options::for($user)->get(ThemeOption::class); // 'light' Options::forget(ThemeOption::class, $user); // back to the default
Documentation
The full documentation — configuration, every feature and its API, and testing — lives on our website: roundly-consulting.com/open-source/docs/options-for-laravel
Release notes are in CHANGELOG.md. To contribute, see the contributing guide.
Support our work
This package is free and open source, built and maintained by Roundly Consulting. If it saves you time, please consider supporting our open-source work — a one-time donation, a monthly pledge on Patreon or a crypto donation helps fund maintenance, new features and new packages.
License
The MIT License (MIT). See LICENSE for details.