mrdth/laravel-model-settings
Composer 安装命令:
composer require mrdth/laravel-model-settings
包简介
Easy to use way to add settings to any Eloquent model
README 文档
README
This package utilizes Eloquents JSON casting to provide a simple way to add settings to any Eloquent model.
Requirements
Installation
You can install the package via composer:
composer require mrdth/laravel-model-settings:^2.0
You can publish the config file with:
php artisan vendor:publish --tag="laravel-model-settings-config"
This is the contents of the published config file:
return [ 'column' => env('MRDTH_MODEL_SETTINGS_COLUMN_NAME', 'settings'), ];
Usage
Migrations
First add the settings column to your model's migration.
... $table->json('settings')->nullable(); ...
For existing models you can create a migration using our artisan command.
php artisan make:msm {model}
To change the column used for settings you can update the MRDTH_MODEL_SETTINGS_COLUMN_NAME in your .env file.
Models
Next add the HasSettings trait to any Eloquent model you want to have settings.
... use Illuminate\Notifications\Notifiable; use Mrdth\LaravelModelSettings\Concerns\HasSettings; class User extends Authenticatable { use HasFactory, Notifiable, HasSettings; ...
Using the model settings
Once the trait is added you can interact with the settings using methods:
$user = User::find(1); $user->hasSetting('use custom avatar'); // false $user->addSetting('use custom avatar', true); $user->hasSetting('use custom avatar'); // true $user->getSetting('use custom avatar'); // true $user->updateSetting('use custom avatar', false); $user->getSetting('use custom avatar'); // false $user->getSetting('non-existent setting'); // null $user->getSetting('non-existent setting', 'default value'); // 'default value' $user->getSettings(); // ['use custom avatar' => false] $user->deleteSetting('use custom avatar'); $user->hasSetting('use custom avatar'); // false $user->deleteSettings();
Query Scopes
You can also query models based on their settings.
$users = User::whereHasSetting('use custom avatar')->get(); // Returns all users with the setting 'use custom avatar' $users = User::whereHasSetting('use custom avatar', true)->get(); // Returns all users with the setting 'use custom avatar' set to true
Testing
composer tests
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
License
The MIT License (MIT). Please see License File for more information.
mrdth/laravel-model-settings 适用场景与选型建议
mrdth/laravel-model-settings 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.11k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 07 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「laravel-model-settings」 「mrdth」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mrdth/laravel-model-settings 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mrdth/laravel-model-settings 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mrdth/laravel-model-settings 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Model Settings for your Laravel app
Laravel model settings for Eloquent
Model Settings for your Laravel app
Model Settings for your Laravel app
Alfabank REST API integration
Model Settings for your Laravel app
统计信息
- 总下载量: 2.11k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-07-26