pkalusek / laravel-model-settings
Simplest way to store model-specific settings/preferences in Laravel
Requires
- php: ^8.1|^8.2|^8.3|^8.4
- illuminate/database: ^10.0|^11.0|^12.0
- illuminate/support: ^10.0|^11.0|^12.0
Requires (Dev)
- orchestra/testbench: ^8.0|^9.0|^10.0
- phpunit/phpunit: ^10.0|^11.0
This package is not auto-updated.
Last update: 2025-08-10 18:20:19 UTC
README
The simplest way to store model-specific settings/preferences in Laravel. This package allows you to easily attach customizable settings to any Eloquent model.
Installation
You can install the package via composer:
composer require pkalusek/laravel-model-settings
The package will automatically register itself via Laravel's package discovery.
Publish and run the migrations:
php artisan vendor:publish --tag="laravel-model-settings-migrations"
php artisan migrate
Usage
Add the HasSettings
trait to any model you want to have settings:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Pkalusek\LaravelModelSettings\Traits\HasSettings; class User extends Model { use HasSettings; // ... your model code }
Now you can easily get, set, and manage settings for your model:
$user = User::find(1); // Set a setting $user->settings->set('theme', 'dark'); $user->settings->set('notifications.email', true); $user->settings->set('preferences.language', 'en'); // Get a setting $theme = $user->settings->get('theme'); // 'dark' $emailNotifications = $user->settings->get('notifications.email'); // true // Get with default value $language = $user->settings->get('preferences.language', 'en'); // Remove a setting $user->settings->forget('theme'); $user->settings->forget('notifications.email');
Features
- Simple API: Easy-to-use methods for getting, setting, and removing settings
- Nested Settings: Support for dot notation for nested settings (e.g.,
notifications.email
) - Default Values: Get settings with fallback default values
- Automatic Cleanup: Empty nested objects are automatically removed
- JSON Storage: Settings are stored as JSON in the database for flexibility
- Polymorphic Relations: Each model instance has its own settings record
Testing
composer test
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.