elnooronline / laravel-settings
Persistent key-value storage for Laravel
Installs: 1 733
Dependents: 1
Suggesters: 0
Security: 0
Stars: 2
Watchers: 3
Forks: 2
Open Issues: 0
Type:package
Requires
- laravel/framework: 5.5.*|5.6.*|5.7.*
- laravelcollective/html: ^5.6
Requires (Dev)
- mockery/mockery: ^1.2
- orchestra/testbench: ~3.0
README
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
composer require elnooronline/laravel-settings
-
Edit config/app.php (Skip this step if you are using laravel 5.5+)
service provider:
Elnooronline\LaravelSettings\Providers\ServiceProvider::class,
class aliases:
'Setting' => Elnooronline\LaravelSettings\Facades\Setting::class,
-
Create settings table
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.
public function boot() { Setting::registerPrefixMethod('country'); ... }
or you can register prefix methods from config file by adding your prefix to
prefix_methods
array
<?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 :
<?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');