Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel User Preferences Laravel Package

robtrehy/laravel-user-preferences

Store and retrieve authenticated users’ preferences in Laravel, saved as JSON in a single DB column (default: users.preferences). Includes config publishing, migration to add the column, and optional caching via Laravel’s cache drivers with customizable keys.

View on GitHub
Deep Wiki
Context7

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

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);

Get a preference

Get the value of a preference for the currently authenticated user:

UserPreferences::get(string $setting);

Reset a preference

Reset a single preference for the currently authenticated user:

UserPreferences::reset(string $setting);

Reset all default preferences

Reset all default preferences for the currently authenticated user:

UserPreferences::setDefaultPreferences();

Get all preferences

Get all preferences for the currently authenticated user:

UserPreferences::all();

Check if a preference exists

Check if the currently authenticated user has a specific preference:

UserPreferences::has(string $setting);

Save a preference

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


New: Arbitrary User Methods

You can now work with preferences for any user instance or ID, not just the currently authenticated user.

Get a preference for a specific user

UserPreferences::getForUser(string $setting, User|int $user);
  • $user can be a User model instance or a user ID.
  • Returns the preference value if set, otherwise the default.

Set a preference for a specific user

UserPreferences::setForUser(string $setting, $value, User|int $user);
  • $user can be a User model instance or a user ID.
  • Saves the preference for that user without affecting the currently authenticated user.

Reset a preference for a specific user

UserPreferences::resetForUser(string $setting, User|int $user);
  • Returns true if the default was restored, false if the preference was deleted.

Check if a specific user has a preference

UserPreferences::hasForUser(string $setting, User|int $user);
  • Returns true if a value exists, false otherwise.

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

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport