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

Technical Evaluation

Architecture Fit

  • Purpose Alignment: The package provides a structured way to manage dynamic settings (e.g., app configurations, feature flags, or user-specific preferences) tied to Eloquent models. This aligns well with Laravel applications requiring flexible, database-backed settings without bloating core logic or using environment files exclusively.
  • Design Philosophy: Leverages Laravel’s Eloquent ORM, making it a natural fit for applications already using Eloquent. The pattern of attaching settings to models (e.g., UserSettings, AppSettings) promotes modularity and separation of concerns.
  • Use Cases:
    • User-specific settings (e.g., theme preferences, notifications).
    • Global/app-wide configurations (e.g., API endpoints, feature toggles).
    • Multi-tenant settings (e.g., tenant-specific rules).
  • Alternatives: Comparable to Laravel’s built-in config() (static) or packages like spatie/laravel-settings (more opinionated). This package offers lightweight, model-centric flexibility.

Integration Feasibility

  • Laravel Compatibility: Explicitly designed for Laravel (v10+ based on last release). Assumes standard Eloquent setup, which reduces friction in most Laravel apps.
  • Database Requirements:
    • Requires a settings table (migration provided) with model_type (polymorphic) and model_id (foreign key). Minimal schema changes if not already using polymorphic relations.
    • Supports JSON or serialized data for settings values (flexible but may require normalization for complex data).
  • API Surface:
    • Fluent interface for setting/getting values (e.g., $user->settings()->set('theme', 'dark')).
    • Caching layer: Optional cache integration (e.g., Redis) for performance.
    • Events: Supports SettingsUpdated events for reactivity.
  • Testing: No built-in testing utilities, but follows Laravel conventions (easy to mock Eloquent models).

Technical Risk

  • Polymorphic Complexity:
    • Risk: Polymorphic relations can introduce query performance overhead if not indexed properly (e.g., model_type + model_id composite index).
    • Mitigation: Ensure database indexes are added and test under load.
  • Data Serialization:
    • Risk: Storing settings as JSON/serialized may complicate searchability or validation of nested data.
    • Mitigation: Use strict typing (e.g., cast attributes) or normalize data into separate tables for critical settings.
  • Migration Path:
    • Risk: Backfilling existing settings from config() or other sources may require custom logic.
    • Mitigation: Plan a phased migration with fallback mechanisms.
  • Package Maturity:
    • Risk: Low stars/release activity (last release in 2025) may indicate limited adoption or unresolved bugs.
    • Mitigation: Review commit history, open issues, and test thoroughly. Consider forking if critical changes are needed.

Key Questions

  1. Data Structure:
    • How will settings values be structured? Will JSON suffice, or do we need relational tables for some settings?
    • Example: {'theme': 'dark', 'notifications': {'email': true, 'sms': false}} vs. normalized themes and notification_preferences tables.
  2. Performance:
    • What is the expected read/write volume for settings? Will caching (Redis) be required?
    • How will polymorphic queries scale with many models/types?
  3. Validation:
    • Are there strict validation rules for settings? If so, how will they be enforced (e.g., model observers, API gates)?
  4. Fallbacks:
    • What fallback behavior is needed if a setting is missing (e.g., default values from config())?
  5. Auditability:
    • Do we need to track who/when settings were changed? If so, extend the model with timestamps or a separate audit table.
  6. Testing Strategy:
    • How will settings be tested in CI? Mock Eloquent models or use a test database with seeded settings?

Integration Approach

Stack Fit

  • Laravel Ecosystem:
    • Eloquent Models: Ideal for apps already using Eloquent (e.g., User, Team). Minimal boilerplate.
    • Service Providers: Easy to bind settings services or repositories for dependency injection.
    • Events/Listeners: Leverage Laravel’s event system for reacting to setting changes (e.g., trigger a webhook when a feature flag updates).
    • Caching: Integrate with Laravel Cache (Redis, Memcached) for performance-critical settings.
  • Database:
    • MySQL/PostgreSQL: Fully supported (polymorphic relations work out of the box).
    • SQLite: May require adjustments for polymorphic indexing.
  • Frontend:
    • APIs: Expose settings via Laravel APIs (e.g., GET /settings/user/{id}).
    • Blade: Directly access settings in views (e.g., @php $theme = auth()->user()->settings->get('theme') @endphp).

Migration Path

  1. Assessment Phase:
    • Audit existing settings (e.g., in config/, hardcoded, or other packages).
    • Identify candidates for model-based settings (e.g., user preferences, tenant configs).
  2. Schema Setup:
    • Publish and run the package’s migration to create the settings table.
    • Add composite indexes on (model_type, model_id) and (key) for performance.
  3. Data Migration:
    • For static configs: Migrate to a AppSettings model (singleton pattern).
    • For user-specific settings: Seed initial values via model observers or a data import script.
    • Example:
      // Migrate from config to AppSettings
      $appSettings = AppSettings::firstOrNew();
      $appSettings->set('api_endpoint', config('services.api.endpoint'));
      $appSettings->save();
      
  4. Application Layer:
    • Replace direct config() calls with the package’s methods where appropriate.
    • Example:
      // Before: config('app.theme')
      // After: auth()->user()->settings->get('theme', config('app.default_theme'))
      
  5. Deprecation:
    • Gradually deprecate old config keys in favor of the new system (use Laravel’s config() caching to avoid breaking changes).

Compatibility

  • Laravel Versions: Tested with Laravel 10+. Ensure compatibility with your version (check composer.json constraints).
  • PHP Versions: Requires PHP 8.1+ (check package’s php config).
  • Dependencies:
    • No hard dependencies beyond Laravel core (uses Eloquent, Events, Cache).
    • Soft dependencies: spatie/laravel-activitylog (if adding audit logs).
  • Third-Party Conflicts:
    • Check for naming collisions (e.g., if another package uses settings table or Settings model).
    • Use namespace aliases if needed (e.g., \Chapdel\Settings\Settings).

Sequencing

  1. Phase 1: Proof of Concept
    • Implement for a non-critical feature (e.g., user theme preferences).
    • Test performance, caching, and edge cases (e.g., missing settings).
  2. Phase 2: Core Settings
    • Migrate global/app settings (e.g., feature flags, API configs).
    • Integrate caching and events.
  3. Phase 3: Full Rollout
    • Replace remaining config() calls with the package.
    • Deprecate old configs and monitor for issues.
  4. Phase 4: Optimization
    • Add indexes, query optimizations, or custom repositories if needed.
    • Implement monitoring for setting access patterns.

Operational Impact

Maintenance

  • Package Updates:
    • Monitor for updates (MIT license allows forks if needed).
    • Low maintenance overhead if the package remains stable.
  • Custom Logic:
    • Extend the package via:
      • Model Observers: Add custom logic on retrieved, saved, or deleted events.
      • Accessors/Mutators: Override or extend setting access (e.g., encrypt sensitive values).
      • Repositories: Create a dedicated SettingsRepository for complex queries.
  • Documentation:
    • Internal docs needed for:
      • How to define new settings (e.g., User::settings()).
      • Fallback logic for missing settings.
      • Caching strategies.

Support

  • Debugging:
    • Common Issues:
      • Polymorphic relation errors (e.g., wrong model_type).
      • Serialization/deserialization bugs (e.g., JSON decode failures).
      • Cache invalidation problems.
    • Tools:
      • Use Laravel Debugbar to inspect queries.
      • Log setting access in development (e.g., Settings::logAccess()).
  • Troubleshooting Workflow:
    1. Verify the settings table structure and indexes.
    2. Check for missing or malformed data in the table.
    3. Validate polymorphic relations (e.g., model_type matches a valid model).
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.
jayeshmepani/jpl-moshier-ephemeris-php
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