benjamincrozat / laravel-settings
Settings in your Laravel application, made easy.
Requires
- php: >=7.1
- illuminate/database: ^5.0|^6.0
- predis/predis: ^1.1
Requires (Dev)
- orchestra/testbench: ^3.5
- phpunit/phpunit: ^7.5
README
Laravel Settings
Settings in your Laravel application, made easy.
Requirements
- PHP 7.1+
- Laravel 5.7+ (it should work on older versions though)
ToDo
- Companion package for Laravel Nova!
Installation
Add the package to your project and publish the migration:
composer require benjamincrozat/laravel-settings php artisan vendor:publish --provider="BC\Laravel\Settings\Providers\SettingsServiceProvider" --tag=migrations php artisan migrate
Usage
Basics
Usage is similar to the config()
helper from Laravel. But instead, settings are stored in your database and cached with the help of whatever driver you are using.
You can set a bunch of settings by passing an array of key-value pairs as the only argument:
settings([ 'app_name' => 'Compu-Global-Hyper-Mega-Net', 'app_description' => "A better Internet company than Flanders'!", ]);
Then, you can access them just like so:
settings('app_name');
Now, let's say you are spinning a new project, which has nothing in the database yet. You can set a fallback value until the setting is created:
settings('app_name', 'Laravel');
You can also get all settings:
settings()->all();
Note that you can also use the Facade if that's what you prefer. Or you could also pull it out from the container via app('settings')
.
Blade
This package even has a Blade directive that you can use just like the helper:
<title>@settings('app_name', 'Laravel')</title>
Artisan
If you need to access or set settings from the command line, I got you covered (thanks appstract/laravel-options for the inspiration):
php artisan settings:list php artisan settings:set <key> <value> php artisan settings:get <key>
Multi-language
Making your settings internationalizable has never been that easy. Let's just extend Laravel Settings and use existing packages that do what we want in a much better way.
First, install spatie/laravel-translatable:
composer require spatie/laravel-translatable
Now, create a Setting
model:
php artisan make:model Setting
Then, open it and make sure it looks like this:
<?php namespace App\Setting; use Spatie\Translatable\HasTranslations; class Setting extends \BC\Laravel\Settings\Setting { use HasTranslations; public $translatable = ['value']; }
Finally, we have to let Laravel Settings know that we want to use our model instead of the built-in version.
php artisan vendor:publish --provider="BC\Laravel\Settings\Providers\SettingsServiceProvider" --tag=config
Then, edit the newly published config/settings.php
as it is below:
<?php return [ 'disable_cache' => false, 'model' => App\Setting::class, ];
Done! Instead of bloating my package with a lot of hard to maintain features, I just take advantage of the formidable Laravel ecosystem!
If you don't know the Laravel Translatable package, make sure to read their documentation first!
Multi-tenancy
If you are using multiple databases for multi-tenancy, that's fine, you can skip this part! But if you're not, let's imagine that you're building a SaaS app and that you want to isolate settings per client. That's really easy! Go to databases/migrations/xxxx_xx_xx_xxxxxx_create_settings_table.php
and add a client_id
column (or anything else depending on what you like):
public function up() { Schema::create('settings', function (Blueprint $table) { $table->unsignedInteger('client_id')->index(); $table->string('key')->index(); $table->text('value')->nullable(); $table->timestamps(); }); }
From there, you can use a custom Setting
model and do whatever you need to do in it! :) (Details here.)