alagaccia / laravel-settings
A Laravel package for managing application settings with a configurable database table
Installs: 7
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/alagaccia/laravel-settings
Requires
- php: ^7.4 || ^8.0
- illuminate/database: ^8.0|^9.0|^10.0|^11.0|^12.0
- illuminate/support: ^8.0|^9.0|^10.0|^11.0|^12.0
README
A Laravel package for managing application settings with a configurable database table.
Installation
Install the package via Composer:
composer require alagaccia/laravel-settings
The service provider will be automatically registered.
Configuration
Publish the configuration file:
php artisan vendor:publish --tag=laravel-settings-config
This will create a config/laravel-settings.php file where you can customize the table name:
return [ 'table_name' => env('SETTINGS_TABLE_NAME', 'settings'), ];
Migration
Publish the migration file:
php artisan vendor:publish --tag=laravel-settings-migrations
Then run the migration:
php artisan migrate
This will create a table (default name: settings) with the following structure:
id- Auto-increment bigint primary keycategory- String(255) with indexlabel- String(255)code- String(255) with unique indexvalue- Long textcreated_at- Timestampupdated_at- Timestamp
Usage
Using the Model
use Alagaccia\LaravelSettings\Models\Setting; // Create a setting Setting::create([ 'category' => 'general', 'label' => 'Site Name', 'code' => 'site_name', 'value' => 'My Awesome Site' ]); // Get a setting by code $siteName = Setting::getByCode('site_name'); // Get a setting with a default value $theme = Setting::getByCode('theme', 'default'); // Set or update a setting Setting::setByCode('site_name', 'New Site Name', 'general', 'Site Name'); // Get all settings in a category $generalSettings = Setting::getByCategory('general');
Using Helper Functions
The package provides convenient global helper functions:
// Get a setting value $siteName = getSetting('site_name'); // Get with default value $theme = getSetting('theme', 'light'); // Set a setting setSetting('site_name', 'New Site Name', 'general', 'Site Name');
Customizing the Table Name
You can customize the table name in the config file or via environment variable:
SETTINGS_TABLE_NAME=app_settings
License
MIT