The simplest way to store model-specific settings/preferences in Laravel. This package allows you to easily attach customizable settings to any Eloquent model.
You can install the package via composer:
composer require pkalusek/laravel-model-settingsThe 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 migrateAdd 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');- 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
composer testPlease see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.