2017 © Pedro Peláez
 

library setting

A Laravel setting manager per user

image

taylornetwork/setting

A Laravel setting manager per user

  • Tuesday, October 10, 2017
  • by taylornetwork
  • Repository
  • 1 Watchers
  • 0 Stars
  • 17 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 0 % Grown

The README.md

Settings for Laravel User and App

This package provides an easy way to access settings for the current logged in user as well as global app settings., (*1)

NOTE: Some features have changed in v2, (*2)

  1. Changes
  2. About
  3. Install
  4. Usage
  5. HasSettings Trait
  6. Config
  7. Alternate Configuration
  8. Extending
  9. License

Changes

v2.1, (*3)

  • Added support for float type values.

v2.0, (*4)

Support for app specific AND user specific settings has been added., (*5)

  • Added AppSetting model and facade
  • Added app_setting helper
  • Added user_setting helper
  • Renamed Setting model and facade to UserSetting
  • Deprecated setting helper
  • Renamed config key setting_model to user_setting_model

If you use the setting helper it will still work but will be removed, so use user_setting instead. The Setting facade has been renamed to UserSetting if you still have code with the Setting facade you can rename the alias in your config/app.php, (*6)

'aliases' => [
    ...
    'Setting' => TaylorNetwork\Setting\Facades\UserSetting::class,
    ...
],

About

This package uses Laravel's auth function to access the logged in user and return their setting for a given key, or a default value. Similar to the way that the config and env functions work., (*7)

When using the HasSettings trait it allows you to access a non-logged in user's settings., (*8)

By default if the default value is not set and a setting cannot be found, null is returned. The default value will also be returned if there is no logged in user., (*9)

It also includes support for app specific settings via AppSetting, (*10)

Install

Via Composer, (*11)

$ composer require taylornetwork/setting

Run database migrations, (*12)

$ php artisan migrate

You can optionally add the HasSettings trait to your user model to access settings directly., (*13)

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use TaylorNetwork\Setting\Traits\HasSettings;

class User extends Authenticatable
{
   use HasSettings;

   // ...
}

Usage

Note: the UserSetting and AppSetting classes extend Illuminate\Database\Eloquent\Model, (*14)

Available Methods

All of these methods are available when using the class or facade., (*15)

guard($guard) [UserSetting only], (*16)

This will allow you to manually set an auth guard before trying to find an active user., (*17)

UserSetting::guard('api')->get('key', 'defaultValue');

get($key, $default = null) [UserSetting and AppSetting], (*18)

Get a setting for the app/logged in user or return the default value., (*19)

UserSetting::get('key');

AppSetting::get('key');

set($key, $value) [UserSetting and AppSetting], (*20)

Creates or updates the setting for the logged in user or app., (*21)

UserSetting::set('someKey', 'someValue');

AppSetting::set('someKey', 'someValue');

Classes

UserSetting:, (*22)

use TaylorNetwork\Setting\UserSetting;

$setting = new UserSetting;
$setting->get('someKey', 'defaultValue');

AppSetting:, (*23)

use TaylorNetwork\Setting\AppSetting;

$setting = new AppSetting;
$setting->get('someKey', 'defaultValue');

Facades

UserSetting:, (*24)


UserSetting::get('someKey'); UserSetting::set('someKey', 'someValue'); UserSetting::guard('api')->get('someKey', 'Default Value!');

AppSetting:, (*25)


AppSetting::get('someKey'); AppSetting::set('someKey', 'someValue');

Helpers

The user_setting and app_setting helper functions are aliases for the get method. The user_setting helper has an optional third parameter to pass a guard or guards., (*26)

Example:, (*27)

// Returns the user's value or null
user_setting('key');

// Returns the app's value or null
app_setting('key');

// Returns the user's value or 'defaultValue'
user_setting('key', 'defaultValue');

// Returns the app's value or 'defaultValue'
app_setting('key', 'defaultValue');

// Returns the user's value or 'defaultValue' using the 'api' guard
user_setting('key', 'defaultValue', 'api');

// Will try and get a value from each guard in order
// If a value other than the default value is found it will immediately return it.
// If none is found after using all the guards in the array, the default value is returned.
user_setting('key', 'defaultValue', ['web', 'api']);

HasSettings Trait

The HasSettings trait adds a settings relation and the following methods to your model, (*28)

setting($key, $default = null), (*29)

This is the same as the get method above BUT can be used for any user not just one that is logged in., (*30)

updateSetting($key, $value), (*31)

This will create or update a setting for the specified user., (*32)

Config

If you need to change the default auth guard or user model, publish config., (*33)

$ php artisan vendor:publish --provider="TaylorNetwork\Setting\SettingServiceProvider" --tag=config

Alternate Configuration

You can modify the settings table to suit your needs, publish the migrations, (*34)

$ php artisan vendor:publish --provider="TaylorNetwork\Setting\SettingServiceProvider" --tag=migrations

Be sure to update the config with appropriate related_column if you change it., (*35)

Extending

You can extend the UserSetting and AppSetting models if you need additional functionality., (*36)

namespace App;

use TaylorNetwork\Setting\UserSetting;

class Setting extends UserSetting
{
    // --
}

Be sure to update the config with the appropriate new user_setting_model or app_setting_model., (*37)

License

The MIT License (MIT). Please see License File for more information., (*38)

The Versions

10/10 2017

dev-master

9999999-dev

A Laravel setting manager per user

  Sources   Download

The Requires

 

The Development Requires

by Sam Taylor

10/10 2017

1.1.1

1.1.1.0

A Laravel setting manager per user

  Sources   Download

The Requires

 

The Development Requires

by Sam Taylor

09/10 2017

1.1.0

1.1.0.0

A Laravel setting manager per user

  Sources   Download

The Requires

 

The Development Requires

by Sam Taylor

23/07 2017

1.0.2

1.0.2.0

A Laravel setting manager per user

  Sources   Download

The Requires

 

by Sam Taylor

16/11 2016

1.0.1

1.0.1.0

A Laravel setting manager per user

  Sources   Download

The Requires

 

by Sam Taylor

16/11 2016

1.0.0

1.0.0.0

A Laravel setting manager per user

  Sources   Download

The Requires

 

by Sam Taylor