Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Settings Laravel Package

chapdel/settings

Add per-model settings to your Laravel app. Store settings in a JSON column, a dedicated table, or Redis via simple traits. Includes defaults plus helpers to get, set, check, remove, and persist settings on any Eloquent model.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require chapdel/settings
    

    Publish the migration and config:

    php artisan vendor:publish --provider="Chapdel\Settings\SettingsServiceProvider"
    php artisan migrate
    
  2. Define a Settings Model Extend Chapdel\Settings\Settings and register it in config/settings.php:

    'models' => [
        'app' => \App\Models\AppSettings::class,
    ],
    
  3. First Use Case Define a setting in your model:

    use Chapdel\Settings\Settings;
    
    class AppSettings extends Settings
    {
        protected $casts = [
            'theme_color' => 'string',
            'max_upload_size' => 'integer',
        ];
    }
    

    Access/update settings:

    $settings = AppSettings::firstOrCreate(['key' => 'app']);
    $settings->theme_color = '#3498db';
    $settings->save();
    

Implementation Patterns

Workflows

  1. Global vs. Scoped Settings Use the key field to differentiate between settings scopes (e.g., app, user:123):

    // Global app settings
    $appSettings = AppSettings::where('key', 'app')->first();
    
    // User-specific settings
    $userSettings = AppSettings::where('key', 'user:'.$user->id)->first();
    
  2. Configuration Caching Cache settings for performance (e.g., in AppServiceProvider):

    public function boot()
    {
        Cache::remember('app_settings', now()->addHours(1), function () {
            return AppSettings::where('key', 'app')->first();
        });
    }
    
  3. Validation & Defaults Override getDefaultValues() in your model:

    protected function getDefaultValues()
    {
        return [
            'theme_color' => '#ffffff',
            'max_upload_size' => 5, // MB
        ];
    }
    
  4. Dynamic Accessors Use accessors for computed settings:

    public function getUploadSizeLimitAttribute()
    {
        return $this->max_upload_size * 1024 * 1024; // Convert MB to bytes
    }
    

Integration Tips

  • Service Providers: Bind settings to the container for easy access:
    $this->app->singleton('settings.app', function () {
        return AppSettings::where('key', 'app')->first();
    });
    
  • Middleware: Fetch user-specific settings early:
    public function handle($request, Closure $next)
    {
        $request->merge(['user_settings' => UserSettings::forUser($request->user())]);
        return $next($request);
    }
    
  • API Responses: Use toArray() or toJson() with custom formatting:
    return response()->json($settings->only(['theme_color', 'upload_size_limit']));
    

Gotchas and Tips

Pitfalls

  1. Key Collisions Ensure key values are unique. Use namespacing (e.g., app:theme, user:123:notifications) to avoid conflicts.

  2. Mass Assignment Risks Explicitly define $fillable to prevent unintended setting overrides:

    protected $fillable = ['theme_color', 'max_upload_size'];
    
  3. Caching Invalidation Clear cache manually after updates:

    Cache::forget('app_settings');
    
  4. Model Events Override saved() or deleted() to trigger side effects (e.g., cache invalidation):

    protected static function boot()
    {
        parent::boot();
        static::saved(function ($model) {
            Cache::forget('app_settings');
        });
    }
    

Debugging

  • Query Logging: Enable Laravel’s query log to inspect settings queries:
    DB::enableQueryLog();
    $settings = AppSettings::where('key', 'app')->first();
    dd(DB::getQueryLog());
    
  • Missing Settings: Use firstOrCreate() with defaults to avoid null values:
    $settings = AppSettings::firstOrCreate(['key' => 'app'], [
        'theme_color' => '#ffffff',
    ]);
    

Extension Points

  1. Custom Storage Override getTable() or use traits to switch databases (e.g., Redis for caching):

    protected $connection = 'redis';
    
  2. Encryption Encrypt sensitive settings using Laravel’s encryption:

    protected $casts = [
        'api_key' => 'encrypted',
    ];
    
  3. Events Dispatch events for settings changes:

    protected static function boot()
    {
        static::saved(function ($model) {
            event(new SettingsUpdated($model));
        });
    }
    
  4. Policy Integration Restrict access to settings via policies:

    class AppSettingsPolicy
    {
        public function update(User $user, AppSettings $settings)
        {
            return $user->isAdmin();
        }
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium