Giter VIP home page Giter VIP logo

laravel-user-preferences's Introduction

Laravel User Preferences

This is a package for Laravel that can be used to store and access preferences of the currently authenticated user. The preferences are stored as JSON in a single database column. The default configuration stores this alongside the user record in the users table.

Installation

  1. Run composer require robtrehy/laravel-user-preferences to include this in your project.
  2. Publish the config file with the following command
    php artisan vendor:publish --provider="RobTrehy\LaravelUserPreferences\UserPreferencesServiceProvider" --tag="config"
    
  3. Modify the published configuration file to your requirements. The file is located at config/user-preferences.php.
  4. Add the preferences column to the database. A migration file is included, just run the following command
    php artisan vendor:publish --provider="RobTrehy\LaravelUserPreferences\UserPreferencesServiceProvider" --tag="migrations" && php artisan migrate
    
    This will add the column defined in your configuration file to the table defined in your configuration file.

Configuration

Open config/user-preferences.php to adjust the packages configuration.

If this file doesn't exist, run php artisan vendor:public --provider="RobTrehy\LaravelUserPreferences\UserPreferencesServiceProvider" --tag="config" to create the default configuration file.

Set table, column, and primary_key to match your requirements. primary_key should be the users id.

Laravel User Preferences uses the Laravel Cache driver to reduce the number of queries on your database. By default Laravel Caches using the file driver. If you wish to disable this, you can use the null driver. The cache key supplied by Laravel User Preferences adds a prefix and suffix to the user's id. You can supply your own prefix and suffix by changing the cache.prefix and cache.suffix configuration values.

In the defaults array you can set your default values for user preferences.

Example configuration

    'database' => [
        'table' => 'users',
        'column' => 'preferences',
        'primary_key' => 'id'
    ],
    'cache' => [
        'prefix' => 'user-',
        'suffix' => '-preferences',
    ],
    'defaults' => [
        'theme' => 'blue',
        'show_welcome' => true
    ]

Usage

Set a preference

Use this method to set a preference for the currently authenticated user

UserPreferences::set(string [setting], [value]);

If a default preference value is set in the config file, the new value must match the type of the default value.

If no default value exists, any value type can be saved. If the default value type is not matched UserPreferences::save() will return an InvalidArgumentException.

Reset all default preferences

Use this method to reset the currently authenticated user to the default preferences found in the config file.

UserPreferences::setDefaultPreferences();

Note: This will not adjust user preferences that do not contain a default value in the config file.

Reset a specific default preference

Use this method to reset a single preference, for the currently authenticated user, to the default value found in your config file, if it exists. If no default value is set in the config file, the preference will be removed from the currently authenticated user's record.

UserPreferences::reset(string [setting]);

This method will return true if a default value was set from the config file. If no default value was found, this method will return false

Get a preference

Use this method to get the value of a preference for the currently authenticated user.

UserPreferences::get(string [setting]);

Get all preferences

Use this method to get all of the currently authenticated user's preferences

UserPreferences::all()

Check if a user has a specific preference

To check if the currently authenticated user has a specific preference set, you can call

UserPreferences::has(string [setting]);

This will return true if a value was found, false if not.

Save a preference

All preferences are saved automatically when UserPreferences::set(); is called.

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

License

This Laravel package is free software distributed under the terms of the MIT license. See LICENSE

laravel-user-preferences's People

Contributors

daniego avatar dependabot[bot] avatar fefo-p avatar reex11 avatar robtrehy avatar

Stargazers

 avatar

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.