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

Laravel Settings Laravel Package

solomon-ochepa/laravel-settings

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require solomon-ochepa/laravel-settings
    

    For Laravel <5.5, manually register the service provider and facade in config/app.php.

  2. Publish Migration (if needed):

    php artisan vendor:publish --provider="SolomonOchepa\Settings\SettingsServiceProvider" --tag="migrations"
    php artisan migrate
    
  3. First Use Case:

    // Store a setting
    Settings::set('app.name', 'My Awesome App');
    
    // Retrieve a setting
    $appName = Settings::get('app.name');
    
    // Get all settings (cached)
    $allSettings = Settings::all();
    

Where to Look First

  • Facade: Settings facade for core operations.
  • Cache: All settings are cached by default (check config/settings.php for cache driver).
  • Migrations: Published migration creates settings table with key, value, and group columns.

Implementation Patterns

Core Workflows

  1. Storing & Retrieving Settings:

    // Set a single setting
    Settings::set('theme.color', '#3498db');
    
    // Get a single setting (with fallback)
    $color = Settings::get('theme.color', '#000000');
    
    // Set multiple settings at once
    Settings::setMultiple([
        'app.timezone' => 'America/New_York',
        'mail.from.address' => 'contact@example.com',
    ]);
    
  2. Grouped Settings:

    // Store in a group (e.g., 'payment')
    Settings::set('payment.gateway', 'stripe', 'payment');
    
    // Retrieve all settings in a group
    $paymentSettings = Settings::getGroup('payment');
    
  3. Environment-Specific Settings:

    // Set environment-specific (e.g., 'production')
    Settings::set('api.key', 'prod_123', null, 'production');
    
    // Retrieve environment-specific
    $apiKey = Settings::get('api.key', null, 'production');
    
  4. Configuration Integration:

    // Override Laravel config with settings
    config(['app.name' => Settings::get('app.name', config('app.name'))]);
    

Integration Tips

  • Service Providers: Bind settings to Laravel config in boot():
    public function boot()
    {
        $this->app['config']->set('app.name', Settings::get('app.name', config('app.name')));
    }
    
  • Middleware: Dynamically adjust app behavior:
    public function handle($request, Closure $next)
    {
        if (Settings::get('maintenance.mode')) {
            abort(503);
        }
        return $next($request);
    }
    
  • API Responses: Serve dynamic settings:
    return response()->json([
        'app' => [
            'name' => Settings::get('app.name'),
            'version' => Settings::get('app.version'),
        ],
    ]);
    

Gotchas and Tips

Pitfalls

  1. Cache Invalidation:

    • Settings are cached by default. Clear cache after manual DB updates:
      php artisan cache:clear
      
    • Or force a refresh:
      Settings::refresh();
      
  2. Serialization Issues:

    • Values are serialized to JSON. Ensure complex data (e.g., arrays) is JSON-serializable:
      Settings::set('user.permissions', ['admin', 'editor']); // Works
      Settings::set('user.permissions', new stdClass());     // Fails (non-serializable)
      
  3. Environment Conflicts:

    • Environment-specific settings override global ones. Test locally to avoid surprises:
      Settings::get('api.key'); // Falls back to default env if not set
      
  4. Migration Conflicts:

    • If the settings table exists, publishing the migration may fail. Skip or manually merge:
      php artisan vendor:publish --tag="migrations" --force
      

Debugging

  • Check Cache: Verify settings are cached:
    dd(Settings::getCacheStore()); // Inspect cached data
    
  • Log Queries: Disable caching temporarily to debug DB issues:
    Settings::disableCache();
    // ... perform operations ...
    Settings::enableCache();
    
  • Validate Keys: Use Settings::has('key') to check existence before retrieval.

Extension Points

  1. Custom Cache Drivers:

    • Override the cache driver in config/settings.php:
      'cache_driver' => 'redis',
      
    • Or extend the SolomonOchepa\Settings\Contracts\CacheStore interface.
  2. Value Encryption:

    • Extend the SolomonOchepa\Settings\SettingsServiceProvider to encrypt sensitive values:
      use Illuminate\Support\Facades\Crypt;
      
      // In a custom provider
      $value = Crypt::encrypt($value);
      Settings::set($key, $value);
      
  3. Event Listeners:

    • Listen for settings.saved or settings.deleted events to trigger side effects:
      Settings::saved(function ($key, $value) {
          Log::info("Setting $key updated to $value");
      });
      
  4. Validation Rules:

    • Add validation logic in a custom facade or service:
      Settings::set('max.users', 100, null, null, function ($value) {
          return is_numeric($value) && $value > 0;
      });
      
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.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui