2017 © Pedro Peláez
 

library laravel-user-settings

Easy boolean settings per user

image

internetcode/laravel-user-settings

Easy boolean settings per user

  • Tuesday, July 17, 2018
  • by Zandervdm
  • Repository
  • 2 Watchers
  • 22 Stars
  • 15 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 1 Open issues
  • 6 Versions
  • 0 % Grown

The README.md

Laravel user settings

Simple and persistent boolean settings per user., (*1)

Build Status MIT Licence PRs Welcome, (*2)

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

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., (*4)

Searching for enabled settings is supported by MySQL as can be found here., (*5)

Usage

Get a setting, (*6)

$user->setting('my_setting');

OR, (*7)

$user->getSetting('my_setting');

Set a setting, (*8)

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

OR, (*9)

$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:, (*10)

/**
 * getSettingFields function.
 * Get the default possible settings for the user. Can be overwritten
 * in the user model.
 *
 * @return array
 */
public function getSettingFields()
{
    return config('user-settings.setting_fields', []);
}

Searching for settings in a query, (*11)

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

Set multiple settings at once, (*12)

$user->setMultipleSettings([
    'my_setting'        => true,
    'my_setting_2'      => false,
]);
$user->save();

Installation

First of all you should require the package using composer:, (*13)

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., (*14)

'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        Illuminate\Auth\AuthServiceProvider::class,

        ...

        Internetcode\LaravelUserSettings\LaravelUserSettingsServiceProvider::class,
    ],

Publish the config and migration files., (*15)

php artisan vendor:publish --tag=config
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., (*16)

On the models where you want to use the settings add the HasSettingsTrait trait., (*17)

<?php

namespace Internetcode\LaravelUserSettings\Tests;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Internetcode\LaravelUserSettings\Traits\HasSettingsTrait;

class User extends Authenticatable
{
    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., (*18)

License

MIT © Zander van der Meer, (*19)

The Versions

17/07 2018

dev-master

9999999-dev

Easy boolean settings per user

  Sources   Download

The Development Requires

by Zander van der Meer

17/07 2018

v1.0.4

1.0.4.0

Easy boolean settings per user

  Sources   Download

The Development Requires

by Zander van der Meer

11/07 2018

v1.0.3

1.0.3.0

Easy boolean settings per user

  Sources   Download

The Development Requires

by Zander van der Meer

07/07 2018

v1.0.2

1.0.2.0

Easy boolean settings per user

  Sources   Download

The Development Requires

by Zander van der Meer

07/07 2018

v1.0.1

1.0.1.0

Easy boolean settings per user

  Sources   Download

The Development Requires

by Zander van der Meer

07/07 2018

v1.0.0

1.0.0.0

Easy boolean settings per user

  Sources   Download

The Development Requires

by Zander van der Meer