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 Setting Laravel Package

akaunting/laravel-setting

Persistent settings for Laravel with database and/or JSON drivers. Includes helper, facade, and Blade directive, supports encryption, caching, auto-save, extra columns, and custom tables/files. Can override Laravel config values for production-friendly tweaks.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require akaunting/laravel-setting
    

    Publish the migration and config:

    php artisan vendor:publish --provider="Akaunting\Setting\SettingServiceProvider"
    

    Run migrations:

    php artisan migrate
    
  2. Define a Setting: Create a setting class (e.g., app/Settings/AppSettings.php):

    namespace App\Settings;
    
    use Akaunting\Setting\Setting;
    
    class AppSettings extends Setting
    {
        public $settings = [
            'site_name' => ['string', 'My App'],
            'maintenance_mode' => ['boolean', false],
            'theme' => ['select', 'light', ['light', 'dark']],
        ];
    }
    
  3. First Use Case: Access a setting in a controller:

    use App\Settings\AppSettings;
    
    public function show()
    {
        $siteName = AppSettings::get('site_name');
        return view('welcome', ['siteName' => $siteName]);
    }
    

Implementation Patterns

Common Workflows

  1. Dynamic Configuration: Use settings to replace hardcoded values (e.g., API keys, feature flags):

    if (AppSettings::get('feature_new_ui')) {
        return view('new-ui');
    }
    
  2. Admin Panel Integration: Create a form to update settings via a controller:

    public function update(Request $request)
    {
        $validated = $request->validate(AppSettings::getValidationRules());
        AppSettings::set($validated);
        return redirect()->back()->with('success', 'Settings updated!');
    }
    
  3. Scoped Settings: Extend Setting for tenant-specific configurations (e.g., TenantSettings):

    class TenantSettings extends Setting
    {
        public $tenantId;
        public $settings = [...];
    }
    
  4. Caching: Enable caching in config/setting.php to reduce DB queries:

    'cache' => true,
    
  5. Validation Rules: Reuse validation rules in forms:

    $rules = AppSettings::getValidationRules();
    

Integration Tips

  • Service Providers: Bind settings to the container for dependency injection:
    $this->app->singleton(AppSettings::class, function () {
        return new AppSettings();
    });
    
  • Events: Listen for SettingUpdated events to trigger side effects (e.g., cache invalidation):
    Setting::updated(function ($setting) {
        Cache::forget('app_config');
    });
    
  • Middleware: Check settings in middleware (e.g., maintenance mode):
    public function handle($request, Closure $next)
    {
        if (AppSettings::get('maintenance_mode')) {
            abort(503);
        }
        return $next($request);
    }
    

Gotchas and Tips

Pitfalls

  1. Migration Conflicts:

    • If you modify $settings after initial migration, run:
      php artisan setting:clear-cache
      
    • For major changes, reset the table manually or use php artisan setting:reset.
  2. Caching Issues:

    • Clear cache after changes:
      php artisan cache:clear
      php artisan config:clear
      
    • Disable caching temporarily for debugging:
      AppSettings::setCache(false);
      
  3. Default Values:

    • Defaults are not overwritten if the setting exists. To reset, use:
      AppSettings::reset();
      
  4. Case Sensitivity:

    • Setting keys are case-sensitive in validation and retrieval.
  5. Mass Assignment:

    • Only allow trusted input in set() calls. Use getValidationRules() to validate:
      $validated = $request->validate(AppSettings::getValidationRules());
      AppSettings::set($validated);
      

Debugging Tips

  • Dump All Settings:
    dd(AppSettings::all());
    
  • Check for Updates:
    composer show akaunting/laravel-setting
    
  • Log Changes: Extend the Setting class to log updates:
    protected function afterSave()
    {
        \Log::info('Setting updated', ['setting' => $this->settings]);
    }
    

Extension Points

  1. Custom Storage: Override the storage engine by binding a custom SettingRepository:

    $this->app->bind('setting.repository', function () {
        return new CustomSettingRepository();
    });
    
  2. Custom Validation: Add logic to getValidationRules():

    public function getValidationRules()
    {
        $rules = parent::getValidationRules();
        $rules['theme'][] = 'required_if:maintenance_mode,true';
        return $rules;
    }
    
  3. Localization: Translate setting labels by extending the Setting class:

    public function getLabel($key)
    {
        return __("settings.{$key}");
    }
    
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