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.
- Run
composer require robtrehy/laravel-user-preferencesto include this in your project. - Publish the config file with the following command
php artisan vendor:publish --provider="RobTrehy\LaravelUserPreferences\UserPreferencesServiceProvider" --tag="config" - Modify the published configuration file to your requirements. The file is located at
config/user-preferences.php. - Add the
preferencescolumn to the database. A migration file is included, just run the following commandThis will add the column defined in your configuration file to the table defined in your configuration file.php artisan vendor:publish --provider="RobTrehy\LaravelUserPreferences\UserPreferencesServiceProvider" --tag="migrations" && php artisan migrate
Open config/user-preferences.php to adjust the package's configuration.
If this file doesn't exist, run
php artisan vendor:publish --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.
'database' => [
'table' => 'users',
'column' => 'preferences',
'primary_key' => 'id'
],
'cache' => [
'prefix' => 'user-',
'suffix' => '-preferences',
],
'defaults' => [
'theme' => 'blue',
'show_welcome' => true
]
Use this method to set a preference for the currently authenticated user:
UserPreferences::set(string $setting, $value);Get the value of a preference for the currently authenticated user:
UserPreferences::get(string $setting);Reset a single preference for the currently authenticated user:
UserPreferences::reset(string $setting);Reset all default preferences for the currently authenticated user:
UserPreferences::setDefaultPreferences();Get all preferences for the currently authenticated user:
UserPreferences::all();Check if the currently authenticated user has a specific preference:
UserPreferences::has(string $setting);All preferences are saved automatically when UserPreferences::set() is called.
You can now work with preferences for any user instance or ID, not just the currently authenticated user.
UserPreferences::getForUser(string $setting, User|int $user);$usercan be aUsermodel instance or a user ID.- Returns the preference value if set, otherwise the default.
UserPreferences::setForUser(string $setting, $value, User|int $user);$usercan be aUsermodel instance or a user ID.- Saves the preference for that user without affecting the currently authenticated user.
UserPreferences::resetForUser(string $setting, User|int $user);- Returns
trueif the default was restored,falseif the preference was deleted.
UserPreferences::hasForUser(string $setting, User|int $user);- Returns
trueif a value exists,falseotherwise.
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
This Laravel package is free software distributed under the terms of the MIT license. See LICENSE