, (*1)
Persistent Settings Manager for Laravel
- Simple key-value storage
- Support multi-level array (dot delimited keys) structure.
- Localization supported.
- Localization using dimsav/laravel-translatable
Installation
-
Install package, (*2)
composer require elnooronline/laravel-settings
-
Edit config/app.php (Skip this step if you are using laravel 5.5+), (*3)
service provider:, (*4)
Elnooronline\LaravelSettings\Providers\ServiceProvider::class,
class aliases:, (*5)
'Setting' => Elnooronline\LaravelSettings\Facades\Setting::class,
-
Create settings table, (*6)
php artisan vendor:publish --provider="Elnooronline\LaravelSettings\Providers\ServiceProvider"
php artisan migrate
Usage
Setting::get('name', 'Computer');
// get setting value with key 'name'
// return 'Computer' if the key does not exists
Setting::all();
// get all settings
Setting::lang('en')->get('name', 'Computer');
// get setting value with key and language
Setting::get('name:en', 'Computer');
// get setting value with key and language
Setting::set('name', 'Computer');
// set setting value by key
Setting::lang('en')->set('name', 'Computer');
// set setting value by key and language
Setting::set('name:en', 'Computer');
// set setting value by key and language
Setting::has('name');
// check the key exists, return boolean
Setting::lang('en')->has('name');
// check the key exists by language, return boolean
Setting::has('name:en');
// check the key exists by language, return boolean
Setting::forget('name');
// delete the setting by key
Setting::lang('en')->forget('name');
// delete the setting by key and language
Setting::forget('name:en');
// delete the setting by key and language
Dealing with array
Setting::get('item');
// return null;
Setting::set('item', ['USB' => '8G', 'RAM' => '4G']);
Setting::get('item');
// return array(
// 'USB' => '8G',
// 'RAM' => '4G',
// );
Setting::get('item.USB');
// return '8G';
Conditions
in your AppServiceProvider
you can register new prefix method., (*7)
public function boot()
{
Setting::registerPrefixMethod('country');
...
}
or you can register prefix methods from config file by adding your prefix to prefix_methods
array, (*8)
<?php
return [
/**
* The model of the settings table.
* if you want to override the model you should extend your settings model from the package model class.
*
* 'model_class' => \App\CustomSetting::class,
*
* class CustomSetting extends \Elnooronline\LaravelSettings\Models\SettingModel
* {
* ...
* }
*/
'model_class' => \Elnooronline\LaravelSettings\Models\SettingModel::class,
/**
* The registered prefix methods.
* Ex:
* 'prefix_methods' => [
* 'country'
* ],
* Usage:
* Setting::country('us')->set('title', 'Website')
*/
'prefix_methods' => [
//
],
];
Usage
Setting::country('us')->set('title', 'Example Website');
Setting::get('name');
// return return 'Example Website';
Setting::country('eg')->set('title', 'عنوان الموقع');
Setting::country('eg')->get('name');
// return return 'عنوان الموقع';
Setting::country('eg')->forget('name');
// delete the setting by key and country
You can also add custom settings to specific model using HasSettings
trait like the following examole :, (*9)
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Elnooronline\LaravelSettings\Models\Traits\HasSettings;
class User extends Authenticatable
{
use HasSettings;
...
}
Usage
$user->settings()->set('key', 'value');
$user->settings()->get('key', 'value');
// Or
Seeting::for($user)->set('key', 'value');
Seeting::for($user)->set('key', 'value');