netkod-bilisim/laravel-model-settings-bag 问题修复 & 功能扩展

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

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

netkod-bilisim/laravel-model-settings-bag

最新稳定版本:v1.0.0

Composer 安装命令:

composer require netkod-bilisim/laravel-model-settings-bag

包简介

You can add simple and flexible, single or multiple settings to your Laravel models.

README 文档

README

Latest Stable Version Total Downloads Dependents License License Scrutinizer StyleCI

Introduction

You can add simple and flexible, single or multiple settings to your Laravel models.

Requirements

  • PHP >= 7.4

Install

composer require netkod-bilisim/laravel-model-settings-bag:"^1"

Integration

Single Setting

1. Add a JSON settings field to your model's migration.

create_users_table.php

Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('email')->unique();
    $table->string('password');
    $table->json('settings')->nullable();
    $table->rememberToken();
    $table->timestamps();
});

2. Use the trait NetkodBilisim\LaravelModelSettingsBag\ModelHasSettingsBag within your model.

User.php

use NetkodBilisim\LaravelModelSettingsBag\ModelHasSettingsBag;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable 
{
    use ModelHasSettingsBag;
     
    // truncated for brevity..
}

Multiple

1. Add a JSON settings field to your model's migration.

create_user_template_settings_table.php

Schema::create('user_template_settings', function (Blueprint $table) {
    $table->id();
    $table->unsignedInteger('user_id');
    $table->json('settings')->nullable();
    $table->rememberToken();
    $table->timestamps();
});

2. Use the trait NetkodBilisim\LaravelModelSettingsBag\ModelHasSettingsBag within your other setting model.

User TemplateSetting.php

use NetkodBilisim\LaravelModelSettingsBag\ModelHasSettingsBag;
use Illuminate\Database\Eloquent\Model;

class UserTemplateSetting extends Model 
{
    use ModelHasSettingsBag;
}

3. Use the trait NetkodBilisim\LaravelModelSettingsBag\ModelHasSettingsBag within your model.

User.php

class User extends Model 
{
    use ModelHasSettingsBag;

    public function templateSettings()
    {
        return $this->hasOne(User TemplateSetting::class);
    }
}

Usage

1.) Get all of the model's settings.

$user = App\User::first();

$user->settings()->all();    // Returns an array of the user's settings.
$user->settings('template')->get();    // Returns an array of a user's template settings.

2.) Get a specific setting.

$user = App\User::first();

$user->settings()->get('some.setting');
$user->settings()->get('some.setting', $defaultValue); // With a default value.

$user->settings('template')->get('layout.boxed');
$user->settings('template')->get('layout.boxed', $defaultValue); // With a default value.

3.) Add or update a setting.

$user = App\User::first();

$user->settings()->update('some.setting', 'new value');
$user->settings('template')->update('layout.boxed', 'new value');

4.) Determine if the model has a specific setting.

$user = App\User::first();

$user->settings()->has('some.setting');
$user->settings('template')->has('layout.boxed');

5.) Remove a setting from a model.

$user = App\User::first();

$user->settings()->delete('some.setting');
$user->settings('template')->delete('layout.boxed');

6.) Set the default settings for a new model.

If you define $defaultSettings as an array property on your model, we will use its value as the default settings for any new models that are created without settings.

User.php

use NetkodBilisim\LaravelModelSettingsBag\ModelHasSettingsBag;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable 
{
    use ModelHasSettingsBag;

    /**
     * The model's default settings.
     * 
     * @var array
     */
    protected $defaultSettings = [
    	'homepage' => '/profile'
    ];

    // truncated for brevity..
}

7.) Specify the settings that are allowed.

If you define $allowedSettings as an array property then only settings which match a value within the $allowedSettings array will be saved on the model.

User.php

use NetkodBilisim\LaravelModelSettingsBag\ModelHasSettingsBag;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable 
{
    use ModelHasSettingsBag;

    /**
     * The model's allowed settings.
     * 
     * @var array
     */
    protected $allowedSettings = ['homepage'];

    // truncated for brevity..
}

8.) Using another method name other than settings()

If you prefer to use another name other than settings , you can do so by defining a $mapSettingsTo property. This simply maps calls to the method (such as config()) to the settings() method.

User.php

use NetkodBilisim\LaravelModelSettingsBag\ModelHasSettingsBag;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable 
{
    use ModelHasSettingsBag;

    /**
     * The settings field name.
     * 
     * @var string
     */
    protected $mapSettingsTo = 'config';
}

License

This package is open source software licensed under the MIT License.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-07-29