Laravel Settings is a package that allows you to easily store and retrieve application settings in your Laravel application. This is particularly useful for managing configurable parameters such as site settings, application configurations, and any other settings that need to be easily accessible throughout your application.
Key Features
- Simple Storage and Retrieval: Store settings in the database and retrieve them easily.
- Configurable: Supports various data types and can be customized for your application’s needs.
- Caching: Efficiently cache settings for improved performance.
- Easy Migration: Comes with built-in migrations to set up your settings table.
Installation
You can install the package via Composer:
1 |
composer require spatie/laravel-settings |
Configuration
After installing the package, you need to publish the configuration file if you want to customize the settings:
1 |
php artisan vendor:publish --provider="Spatie\Settings\SettingsServiceProvider" |
Migration
You may need to create a migration for the settings table. You can use the built-in command:
1 |
php artisan settings:table |
This command generates a migration file where you can customize the fields for your settings. Once you have reviewed the migration, run the migration to create the settings table:
1 |
php artisan migrate |
Basic Usage
Storing Settings
You can store settings in your application using the Settings
facade:
1 2 3 4 |
use Spatie\Settings\Settings; Settings::set('site_name', 'My Awesome Site'); Settings::set('site_description', 'This is a fantastic website.'); |
Retrieving Settings
To retrieve settings, you can use the same facade:
1 2 |
$siteName = Settings::get('site_name'); $siteDescription = Settings::get('site_description'); |
Checking if a Setting Exists
You can check if a specific setting exists:
1 2 3 |
if (Settings::has('site_name')) { $siteName = Settings::get('site_name'); } |
Defining Settings with Classes
To organize your settings better, you can create a class to define your settings. This helps in type hinting and organizing settings more clearly.
- Create a settings class:
php
12345678910namespace App\Settings;use Spatie\Settings\Settings;class SiteSettings extends Settings{public string $site_name;public string $site_description;// Add more settings as needed} - Register the settings class in a service provider (e.g.,
AppServiceProvider
):php
12345678use App\Settings\SiteSettings;public function register(){$this->app->singleton(SiteSettings::class, function () {return new SiteSettings();});} - Use the settings class in your application:
php
1234use App\Settings\SiteSettings;$settings = app(SiteSettings::class);$siteName = $settings->site_name;
Caching Settings
To improve performance, you can cache your settings. You can set a cache duration in your configuration file. Here’s an example of how to cache settings:
1 2 3 |
$settings = Settings::cache('site_settings', 60, function () { return Settings::all(); }); |
This caches the settings for 60 minutes.
Conclusion
Laravel Settings provides an efficient way to manage application settings in your Laravel applications. With its simple interface and the ability to define settings using classes, you can easily store and retrieve configuration parameters as needed. This package also helps in keeping your settings organized and maintainable.