项目作者: Taronyuu

项目描述 :
Simple and persistent boolean settings per user
高级语言: PHP
项目地址: git://github.com/Taronyuu/laravel-user-settings.git
创建时间: 2018-07-07T11:29:38Z
项目社区:https://github.com/Taronyuu/laravel-user-settings

开源协议:MIT License

下载


Laravel user settings

Simple and persistent boolean settings per user.

Build Status
MIT Licence
PRs Welcome

This package has been developed to help you store simple boolean settings (true/false or yes/no settings) per user.

Features

  • Only 1 additional column for multiple settings.
  • Settings are stored as binary.
  • Can be used on all models.
  • Customizable.
  • Fast.

Background

Laravel user settings only requires 1 additional column (bigint) per entity. All settings are stored in this column as a binary value. By using the bitwise operators in PHP we are able to store multiple settings in a single column without extra coding/decoding or multiple queries.

Searching for enabled settings is supported by MySQL as can be found here.

Usage

Get a setting

  1. $user->setting('my_setting');

OR

  1. $user->getSetting('my_setting');

Set a setting

  1. $user->setting('my_setting', true);

OR

  1. $user->setSetting('my_setting', true);

Overriding a list of allowed setting for the entity.
A global list of settings can be found in the user-settings.php config file, if you want to override these settings per model you can override the following method:

  1. /**
  2. * getSettingFields function.
  3. * Get the default possible settings for the user. Can be overwritten
  4. * in the user model.
  5. *
  6. * @return array
  7. */
  8. public function getSettingFields()
  9. {
  10. return config('user-settings.setting_fields', []);
  11. }

Searching for settings in a query

  1. $user = (new User())->whereSetting('my_setting')->first();

Set multiple settings at once

  1. $user->setMultipleSettings([
  2. 'my_setting' => true,
  3. 'my_setting_2' => false,
  4. ]);
  5. $user->save();

Installation

First of all you should require the package using composer:

  1. composer require internetcode/laravel-user-settings

Afterwards you can add the service provider to your providers array. This is optional since it is already auto discovered by Laravel.

  1. 'providers' => [
  2. /*
  3. * Laravel Framework Service Providers...
  4. */
  5. Illuminate\Auth\AuthServiceProvider::class,
  6. ...
  7. Internetcode\LaravelUserSettings\LaravelUserSettingsServiceProvider::class,
  8. ],

Publish the config and migration files.

  1. php artisan vendor:publish --tag=config
  2. php artisan vendor:publish --tag=migrations

Please note that the newly created migration file defaults to a settings column on the user model. Feel free to change that, or add multiple tables.

On the models where you want to use the settings add the HasSettingsTrait trait.

  1. <?php
  2. namespace Internetcode\LaravelUserSettings\Tests;
  3. use Illuminate\Notifications\Notifiable;
  4. use Illuminate\Foundation\Auth\User as Authenticatable;
  5. use Internetcode\LaravelUserSettings\Traits\HasSettingsTrait;
  6. class User extends Authenticatable
  7. {
  8. use HasSettingsTrait;

Caveats

  • Never change the order of the settings in the setting_fields array. Every field in here is converted based on the index of the field. Therefore changing the order/index of your setting, will result in invalid settings being true or false.

Bugs / Issues / Ideas

Please create an issue using the issue tracker or drop us an email.

License

MIT © Zander van der Meer