internetcode/laravel-user-settings
Composer 安装命令:
composer require internetcode/laravel-user-settings
包简介
Easy boolean settings per user
README 文档
README
Simple and persistent boolean settings per user.
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_fieldsarray. 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
internetcode/laravel-user-settings 适用场景与选型建议
internetcode/laravel-user-settings 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.57k 次下载、GitHub Stars 达 37, 最近一次更新时间为 2018 年 07 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 internetcode/laravel-user-settings 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 internetcode/laravel-user-settings 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 7.57k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 37
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2018-07-07