Backpack Settings

repository·main·Indexed 43 days ago

https://github.com/laravel-backpack/settings

An administrative interface for managing application settings via a database-backed store. It allows administrators to change application behavior and Laravel configurations through a GUI without modifying code, providing a Setting model and integration with the Laravel config() helper.

Tokens
2.1K
Snippets
7
Records
11
Agent score
54%

What's inside Backpack Settings

  1. How to overwrite Backpack\Settings functionality

    main

    If you need to customize the core behavior (routes, controllers, or models) of the package:

    1. Routes: Create a routes/backpack/settings.php file. The package will prioritize this file over its internal routes.
    2. Controllers/Models: Create your own classes that extend the package's controllers or models. Use these custom classes within your custom routes file.
  2. Add new settings to the database

    main

    There is no UI for adding new settings; they must be added directly to the database (e.g., via migrations or seeders).

    The settings table schema is:

    • id: Primary key
    • key: The unique identifier (e.g., contact_email)
    • name: Human-readable label
    • description: Explanation of the setting
    • value: The actual setting value
    • field: Crucial. A JSON string containing the Backpack CRUD field configuration. The name key inside this JSON must be set to "value" for the setting to work correctly.
    • active: Boolean (1 or 0)
    • created_at / updated_at
  3. Install Backpack\Settings

    main

    Follow these steps to install the package and set up the database.

    Note: The default table name is settings. If you need to change it, publish the config first and modify the values in config/backpack/settings.php before running migrations.

    1. Install via Composer:
      composer require backpack/settings
    2. (Optional) Customize table/migration names:
      php artisan vendor:publish --provider="Backpack\Settings\SettingsServiceProvider" --tag="config"
      Edit config/backpack/settings.php if needed.
    3. Publish and run migrations:
      php artisan vendor:publish --provider="Backpack\Settings\SettingsServiceProvider"
      php artisan migrate
    4. (Optional) Add a menu item:
      • For Backpack v6:
        php artisan backpack:add-menu-content "<x-backpack::menu-item title='Settings' icon='la la-cog' :link=\"backpack_url('setting')\" />"
      • For Backpack v5 or v4:
        php artisan backpack:add-sidebar-content "<li class='nav-item'><a class='nav-link' href='{{ backpack_url('setting') }}'><i class='nav-icon la la-cog'></i> <span>Settings</span></a></li>"
    5. (Optional) Seed dummy data:
      php artisan db:seed --class="Backpack\Settings\database\seeds\SettingsTableSeeder"
    composer require backpack/settings
    php artisan vendor:publish --provider="Backpack\Settings\SettingsServiceProvider"
    php artisan migrate
  4. Override Laravel configurations using settings

    main

    You can make standard Laravel configuration values adjustable via the Settings GUI.

    1. Create the setting entry in your database. Ensure the field column contains a JSON string where the name key is "value". Example for a checkbox setting show_powered_by:

      • key: show_powered_by
      • value: 1
      • field: {"name":"value","label":"Value","type":"checkbox"}
    2. Apply the override in app/Providers/AppServiceProvider.php within the boot method to ensure the configuration is updated during the application lifecycle.

    // app/Providers/AppServiceProvider.php
    
    public function boot()
    {
        $this->overrideConfigValues();
    }
    
    protected function overrideConfigValues()
    {
        $config = [];
        // Check if the setting exists in the database via the settings helper
        if (config('settings.show_powered_by')) {
            // Map the setting value to the target Laravel config key
            $config['backpack.ui.show_powered_by'] = config('settings.show_powered_by') == '1';
        }
        config($config);
    }
    // Inside AppServiceProvider
    protected function overrideConfigValues()
    {
        $config = [];
        if (config('settings.show_powered_by')) {
            $config['backpack.ui.show_powered_by'] = config('settings.show_powered_by') == '1';
        }
        config($config);
    }
  5. Access settings via the Laravel Config facade

    main

    The package automatically binds all settings stored in your database to the Laravel configuration repository during the application boot process. This allows you to retrieve any setting using the standard config() helper or the Config facade.

    To ensure your settings are organized, you can define a config_prefix in config/backpack/settings.php. If a prefix is set, the configuration key will be prefix.setting_key. If no prefix is set, the key will simply be the key stored in the database.

    // If config('backpack.settings.config_prefix') is 'settings'
    // and you have a setting with key 'contact_email' and value 'admin@example.com'
    
    $email = config('settings.contact_email');
  6. Customize the settings routes

    main

    The package loads its routes from a default file located at /routes/backpack/settings.php within the package.

    If you want to override the package's default routes with your own custom logic, create a file at routes/backpack/settings.php in your application's root directory. The service provider will detect this file and use it instead of the vendor version.

  7. Retrieve settings values in code

    main

    You can access settings values similarly to how you use Laravel's config() helper. The values are stored in the database and fetched on boot.

    Use the Setting model or the config() helper with the settings prefix.

    Setting::get('contact_email');
    // or
    Config::get('settings.contact_email');
  8. Customize the Setting model and route

    main

    The SettingCrudController uses configuration values to determine which model to manage and which route to use. You can override these in config/backpack/settings.php using the following keys:

    • model: The FQCN of the model used for settings (defaults to \Backpack\Settings\app\Models\Setting::class).
    • route: The URL path for the settings CRUD (used by backpack_url()).
  9. Retrieve a setting value with Setting::get()

    main

    Use the Setting::get($key, $default) static method to retrieve a value from the settings table. The $key corresponds to the key column in your database. If the key does not exist, the method returns the provided $default value.

    // Returns the value for the key 'site_name'
    $name = 
    \Backpack\Settings\app\Models\Setting::get('site_name');
    
    // Returns 'Default Name' if 'site_name' is not found
    $name = 
    \Backpack\Settings\app\Models\Setting::get('site_name', 'Default Name');
  10. Update a setting value with Setting::set()

    main

    Use the Setting::set($key, $value) static method to update a setting's value in the database.

    Behavior details:

    • It updates the value column for the record matching the provided $key.
    • It automatically attempts to sync the new value to the Laravel configuration using the backpack.settings.database_prefix defined in your config. If a prefix is configured (e.g., settings.), the config key will be settings.your_key.
    • Returns true if the configuration was successfully updated to match the new value, otherwise returns false.
    // Updates the database and the runtime config
    \Backpack\Settings\app\Models\Setting::set('site_name', 'My New Site');
  11. Configure the Setting model and alias

    main

    By default, the package uses \Backpack\Settings\app\Models\Setting::class as the Eloquent model for settings. You can swap this for your own custom model by updating the model key in config/backpack/settings.php.

    The package also registers a Setting alias in the Laravel AliasLoader, which points to whatever model class is defined in your configuration.

    // In config/backpack/settings.php
    'model' => \App\Models\MyCustomSetting::class,
    
    // You can then use the alias anywhere in your app
    Setting::all();