Git Product home page Git Product logo

laravel-user-settings's Introduction

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

$user->setting('my_setting');

OR

$user->getSetting('my_setting');

Set a setting

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

OR

$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:

/**
 * 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

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

Set multiple settings at once

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

Installation

First of all you should require the package using composer:

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.

'providers' => [

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

        ...

        Internetcode\LaravelUserSettings\LaravelUserSettingsServiceProvider::class,
    ],

Publish the config and migration files.

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.

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

<?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.

License

MIT © Zander van der Meer

laravel-user-settings's People

Contributors

taronyuu avatar unamatasanatarai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

laravel-user-settings's Issues

Default argument

When retrieving a setting make it possible to apply a second argument to pass a default value. (And return null if the setting is not found and no default value is passed)

Pass array to setting

Ability to pass array to setting method so you can also set values, and set multiple values at once.

helpers.php

Remove helpers.php or add new line in helpers.php below <?php

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.