hosmelq / laravel-model-preferences
Per-model preferences for Laravel with defaults and validation.
Package info
github.com/hosmelq/laravel-model-preferences
pkg:composer/hosmelq/laravel-model-preferences
Requires
- php: ^8.2
- illuminate/contracts: ^11.0 || ^12.0
- illuminate/database: ^11.0 || ^12.0
- illuminate/support: ^11.0 || ^12.0
- spatie/laravel-package-tools: ^1.92
- thecodingmachine/safe: ^3.3
Requires (Dev)
- ergebnis/composer-normalize: ^2.49
- larastan/larastan: ^3.9
- laravel/pint: ^1.27
- orchestra/pest-plugin-testbench: ^3.2
- orchestra/testbench: ^9.0 || ^10.0
- pestphp/pest: ^3.8
- pestphp/pest-plugin-laravel: ^3.2
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- rector/rector: ^2.3
- rector/type-perfect: ^2.1
- shipmonk/composer-dependency-analyser: ^1.8
- spaze/phpstan-disallowed-calls: ^4.7
- thecodingmachine/phpstan-safe-rule: ^1.4
- ticketswap/phpstan-error-formatter: ^1.2
- tomasvotruba/type-coverage: ^2.1
README
This package provides a simple API for storing preferences on Eloquent models.
You can define defaults and validation rules, and choose where preferences are stored.
Here's a quick example:
use HosmelQ\ModelPreferences\Contracts\HasPreferences; use HosmelQ\ModelPreferences\Models\Concerns\InteractsWithPreferences; use HosmelQ\ModelPreferences\Support\PreferencesConfig; use Illuminate\Database\Eloquent\Model; class User extends Model implements HasPreferences { use InteractsWithPreferences; public function preferencesConfig(): PreferencesConfig { return PreferencesConfig::configure()->withDefaults([ 'theme' => 'system', ]); } } $user = User::query()->first(); $user->preferences()->set('theme', 'dark'); $theme = $user->preferences()->get('theme');
You can customize defaults, validation rules, and storage options:
use HosmelQ\ModelPreferences\Support\PreferencesConfig; public function preferencesConfig(): PreferencesConfig { return PreferencesConfig::configure() ->withColumn('settings') ->withDefaults([ 'theme' => 'system', ]) ->withDriver('column') ->withRules([ 'theme' => ['in:light,dark,system'], ]); }
Documentation
All documentation is available on the documentation site.
Testing
composer test
Support
If this package is useful in your project, consider starring the repository and sharing feedback or improvements.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Contributions are welcome. Please open a PR with tests and clear context for the change.
Security
If you discover any security related issues, please email hosmelq@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.