danielemontecchi/laravel-scoped-settings

A Laravel package for global and model-scoped application settings.

v1.1.0 2025-04-17 15:54 UTC

This package is auto-updated.

Last update: 2025-04-17 16:06:56 UTC


README

Latest Version on Packagist Total Downloads GitHub Tests Action Status codecov License: MIT Documentation

Laravel Scoped Settings is a lightweight Laravel package that provides a simple and elegant way to manage global and model-scoped application settings.

  • โšก Supports global settings and per-model scoped settings
  • ๐ŸŽฏ API via Facade and setting() helper
  • โœ… Includes artisan commands and full test suite
  • ๐Ÿš€ Optional per-scope caching built into set() and get()

๐Ÿ“ฆ Installation

You can install the package via Composer:

composer require danielemontecchi/laravel-scoped-settings

To customize default cache behavior, publish the config file:

php artisan vendor:publish --tag="laravel-scoped-settings-config"

To publish and run the migrations:

php artisan vendor:publish --tag="laravel-scoped-settings-migrations"
php artisan migrate

๐Ÿ›  Usage

// Global settings
setting()->set('site.name', 'My App');
$siteName = setting()->get('site.name');

// Set with optional caching (global or scoped)
setting()->set('site.name', 'My App', 3600); // Cache for 1 hour

// If TTL is omitted, uses default from config (or disables cache)
setting()->set('site.name', 'My App'); // No cache if config is null

// Check if a setting exists (ignores fallback)
if (setting()->has('site.name')) {
    // the value is explicitly set
}

// Scoped settings (e.g. per user)
setting()->for($user)->set('dashboard.layout', 'compact');
$layout = setting()->for($user)->get('dashboard.layout');

You can also retrieve all flat or grouped settings:

setting()->all(); // ['site.name' => 'My App']
setting()->for($user)->group('dashboard'); // ['layout' => 'compact']

Clear a specific key:

setting()->forget('site.name');

๐Ÿ›  Artisan Commands

php artisan settings:list     # Show all settings in CLI
php artisan settings:clear    # Clear all settings
php artisan settings:export   # Export settings to JSON
php artisan settings:import   # Import settings from JSON

๐Ÿงช Testing

./vendor/bin/pest

Coverage reports are generated via phpunit.xml config and uploaded to Codecov.

๐Ÿ“š Documentation

Full documentation available at:

๐Ÿ‘‰ https://danielemontecchi.github.io/laravel-scoped-settings

Includes:

  • Installation & Configuration
  • Usage Examples
  • Scoping Strategies
  • Artisan Commands
  • Advanced Tips

๐Ÿ“ License

The MIT License (MIT). Please see License File for more information.