PHPackages                             internetcode/laravel-user-settings - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. internetcode/laravel-user-settings

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

internetcode/laravel-user-settings
==================================

Easy boolean settings per user

v1.0.4(7y ago)377.6k2[1 issues](https://github.com/Taronyuu/laravel-user-settings/issues)PHP

Since Jul 7Pushed 7y ago1 watchersCompare

[ Source](https://github.com/Taronyuu/laravel-user-settings)[ Packagist](https://packagist.org/packages/internetcode/laravel-user-settings)[ RSS](/packages/internetcode-laravel-user-settings/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (1)Dependencies (2)Versions (6)Used By (0)

Laravel user settings
=====================

[](#laravel-user-settings)

> Simple and persistent boolean settings per user.

[![Build Status](https://camo.githubusercontent.com/d342bcdd76236cbf912fb9bf0c57d9f58eefb65cb7dd4bb069819a54afb2b63c/68747470733a2f2f7472617669732d63692e6f72672f496e7465726e6574636f646568712f6c61726176656c2d757365722d73657474696e67732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Internetcodehq/laravel-user-settings)[![MIT Licence](https://camo.githubusercontent.com/fabb40ab22588a0746bb0916ed92739171bde7fb31f281c627aa588bcba62cc2/68747470733a2f2f6261646765732e66726170736f66742e636f6d2f6f732f6d69742f6d69742e7376673f763d313033)](https://opensource.org/licenses/mit-license.php)[![PRs Welcome](https://camo.githubusercontent.com/25b3e6d0d42c98de74a98cbb4d149a1c09020cf6d1361993b72d7d5b8ffed363/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5052732d77656c636f6d652d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](http://makeapullrequest.com)

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

### Features

[](#features)

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

### Background

[](#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](http://php.net/manual/en/language.operators.bitwise.php) 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.](https://dev.mysql.com/doc/refman/8.0/en/bit-functions.html)

### Usage

[](#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

[](#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.

```
