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

Setting Laravel Package

larapacks/setting

Laravel package for storing and retrieving application settings with a simple API. Manage key/value configuration in your database, access values via helpers or facades, and keep defaults in code while allowing runtime overrides for per-app customization.

Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require larapacks/setting
    

    Publish the migration and config:

    php artisan vendor:publish --provider="Larapacks\Setting\SettingServiceProvider" --tag="migrations"
    php artisan vendor:publish --provider="Larapacks\Setting\SettingServiceProvider" --tag="config"
    

    Run the migration:

    php artisan migrate
    
  2. First Use Case: Define a setting via Tinker or a seed:

    use Larapacks\Setting\Facades\Setting;
    
    Setting::set('app.max_upload_size', '10MB');
    

    Retrieve it in code:

    $maxUploadSize = Setting::get('app.max_upload_size');
    
  3. Where to Look First:

    • Facade API: Larapacks\Setting\Facades\Setting (e.g., Setting::get(), Setting::set()).
    • Config: config/setting.php (adjust cache drivers, storage, etc.).
    • Migrations: database/migrations/[timestamp]_create_settings_table.php (customize columns if needed).

Implementation Patterns

Core Workflows

  1. Feature Flags:

    if (Setting::get('features.new_ui')) {
        return view('new-ui');
    }
    
  2. Dynamic Configuration: Replace hardcoded values with settings:

    $apiRateLimit = Setting::get('api.rate_limit', 100); // Default fallback
    
  3. Environment-Specific Overrides: Useful for staging/production tweaks without redeploying:

    Setting::set('app.debug_mode', env('APP_DEBUG') ? 'true' : 'false');
    
  4. Caching for Performance: Enable caching in config/setting.php:

    'cache' => [
        'driver' => 'file', // or 'redis', 'database'
        'prefix' => 'setting_',
    ],
    

    Clear cache when settings change:

    Setting::clearCache();
    

Integration Tips

  • Validation: Use Laravel’s validation rules when setting values:
    $validated = Setting::set('app.timeout', request()->input('timeout'), [
        'numeric',
        'min:1',
        'max:300',
    ]);
    
  • Events: Listen for setting changes via SettingUpdated event:
    Setting::updated(function ($key, $oldValue, $newValue) {
        Log::info("Setting '$key' changed from '$oldValue' to '$newValue'");
    });
    
  • Seeding: Populate defaults in a seeder:
    Setting::set('app.default_currency', 'USD');
    Setting::set('app.support_email', 'support@example.com');
    

Gotchas and Tips

Pitfalls

  1. Cache Invalidation:

    • Forgetting to clear cache after manual updates (e.g., via php artisan setting:clear-cache).
    • Fix: Use Setting::clearCache() or configure auto-clearing in config/setting.php.
  2. Type Mismatches:

    • Settings stored as strings may cause issues if treated as booleans/numbers.
    • Fix: Cast values explicitly:
      $isEnabled = (bool) Setting::get('features.dark_mode');
      
  3. Overwriting Defaults:

    • Accidentally overwriting package defaults in config/setting.php.
    • Fix: Extend the config instead of replacing:
      return [
          'storage' => [
              'driver' => 'database',
          ],
          // Override only what's needed
      ];
      
  4. Migration Conflicts:

    • Customizing the settings table schema after initial migration.
    • Fix: Drop and recreate the table or use a new migration with Schema::table().

Debugging Tips

  • Check Storage Driver: Verify settings are saved in the expected location (e.g., database or cache).
    Setting::getStorage()->getDriver(); // Inspect current driver
    
  • Log Missing Settings: Use a fallback with logging:
    $value = Setting::get('app.unknown_setting', null);
    if (is_null($value)) {
        Log::warning("Missing setting: app.unknown_setting");
    }
    
  • Test Cache Behavior: Disable caching temporarily in config/setting.php to isolate issues:
    'cache' => [
        'driver' => 'null',
    ],
    

Extension Points

  1. Custom Storage: Implement Larapacks\Setting\Contracts\SettingStorage for alternative backends (e.g., S3, external APIs):

    class S3SettingStorage implements SettingStorage {
        // ...
    }
    

    Register in config/setting.php:

    'storage' => [
        'driver' => 's3',
        'class' => \App\Services\S3SettingStorage::class,
    ],
    
  2. Scoped Settings: Use middleware to set request-specific scopes:

    Setting::scope('tenant', $tenantId);
    $value = Setting::get('app.theme'); // Resolves to tenant-scoped value
    
  3. Encrypted Settings: Combine with Laravel’s encryption for sensitive values:

    $encryptedValue = Setting::get('app.api_key');
    $decryptedValue = decrypt($encryptedValue);
    
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4