mrdth/laravel-model-settings 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mrdth/laravel-model-settings

最新稳定版本:v1.2.0

Composer 安装命令:

composer require mrdth/laravel-model-settings

包简介

Easy to use way to add settings to any Eloquent model

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package utilizes Eloquents JSON casting to provide a simple way to add settings to any Eloquent model.

Installation

You can install the package via composer:

composer require mrdth/laravel-model-settings

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 methodsL

$user = User::find(1);
$user->hasSetting('use custom avatar'); // false

$user->setSetting('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.

统计信息

  • 总下载量: 1.96k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-07-26