承接 ruangdeveloper/laravel-settings 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

ruangdeveloper/laravel-settings

Composer 安装命令:

composer require ruangdeveloper/laravel-settings

包简介

Manage model or application settings in database

README 文档

README

Introducing "Laravel Settings" – a powerful and flexible Laravel package designed to simplify the management of application settings, both at the global and model-specific levels. With this package, developers can effortlessly store, retrieve, and customize settings within the database, enhancing the configuration and flexibility of Laravel-based projects.

Whether you need to manage site-wide configurations, user preferences, or any other form of customizable settings, "Laravel Settings" provides an elegant solution to streamline the process. It offers a clean and intuitive API, ensuring that you can get started quickly without any steep learning curve.

Key Features:

  • Global and Model-Specific Settings: "Laravel Settings" allows you to define both global settings, which apply to your entire application, and model-specific settings, which are associated with specific Eloquent models.

  • Easy Configuration: You can define settings in a configuration file, making it a breeze to set up and manage your application's settings.

  • Customizable: The package offers the flexibility to create custom setting types, ensuring that you can handle various data types, validation rules, and display options.

  • Eloquent Integration: "Laravel Settings" seamlessly integrates with Laravel's Eloquent ORM, enabling you to link settings to specific database records.

Requirements

  • Laravel ^11.0 || ^12.0

Installation:

To get started with "Laravel Settings," follow these simple installation steps:

Install the package via Composer:

composer require ruangdeveloper/laravel-settings

Publish the configuration file:

php artisan vendor:publish --provider="RuangDeveloper\LaravelSettings\LaravelSettingsServiceProvider" --tag="config"

Configure the settings in config/laravel-settings.php according to your application's requirements.

Publish the migration file

php artisan vendor:publish --provider="RuangDeveloper\LaravelSettings\LaravelSettingsServiceProvider" --tag="migrations"

Usage

Let's take a quick look at how you can use "Laravel Settings" to manage a global setting for your application.

Set a global setting

use RuangDeveloper\LaravelSettings\Facades\Settings;

// setting the global site title
Settings::set('site_title', 'Your Awesome Website');

Get a global setting

use RuangDeveloper\LaravelSettings\Facades\Settings;

// retrieve the global site title
$siteTItle = Settings::get('site_title');

// you may want to add a default fallback value if the setting
// with provided key doesn't exists in the database
$siteTitle = Settings::get('site_title', 'Your Default Awesome Website');

Get a setting and cast it to a specific type

use RuangDeveloper\LaravelSettings\Enums\Type;
use RuangDeveloper\LaravelSettings\Facades\Settings;

// retrieve the global site title
$siteTitle = Settings::getAs('site_title', Type::String);

// you may want to add a default fallback value if the setting
// with provided key doesn't exists in the database
$siteTitle = Settings::getAs('site_title', Type::String, 'Your Default Awesome Website');

Available types:

  • String
  • Integer
  • Float
  • Boolean
  • Array
  • Object

You can also use the following methods to get a setting and cast it to a specific type:

  • Settings::getString('key', $default)
  • Settings::getInteger('key', $default)
  • Settings::getFloat('key', $default)
  • Settings::getBoolean('key', $default)
  • Settings::getArray('key', $default)
  • Settings::getObject('key', $default)

Delete a global setting

Now, if you want to delete the setting

use RuangDeveloper\LaravelSettings\Facades\Settings;

Settings::delete('site_title');

Forget a global setting (deprecated, use delete instead)

Now, if you want to delete the setting

use RuangDeveloper\LaravelSettings\Facades\Settings;

Settings::forget('site_title');

Model Specific Setting

This package allow you to link the setting to a specific model. For example, you may want to store user's preferences.

Model Configuration The first step before linking the setting to a specific model, you need to configure your model to use the HasSettings traits.

use Illuminate\Database\Eloquent\Model;
use RuangDeveloper\LaravelSettings\Traits\HasSettings;

class User extends Model
{
    use HasSettings; // use the HasSettings trait

    protected $guarded = [];
}

Now, you can use the setting for an user.

Set a setting

$user = App\Models\User::find(1);
$user->setSetting('subscribe_newsletter', true);

Get a setting

$user = App\Models\User::find(1);
$isSubscribed = $user->getSetting('subscribe_newsletter');

// you may want to add a default fallback value if the setting
// with provided key doesn't exists in the database
$isSubscribed = $user->getSetting('subscribe_newsletter', false);
//

Get a setting and cast it to a specific type

use RuangDeveloper\LaravelSettings\Enums\Type;
use RuangDeveloper\LaravelSettings\Facades\Settings;

$user = App\Models\User::find(1);
$isSubscribed = $user->getSettingAs('subscribe_newsletter', Type::Boolean);

// you may want to add a default fallback value if the setting
// with provided key doesn't exists in the database
$isSubscribed = $user->getSettingAs('subscribe_newsletter', Type::Boolean, false);

Available types:

  • String
  • Integer
  • Float
  • Boolean
  • Array
  • Object

You can also use the following methods to get a setting and cast it to a specific type:

  • $yourModel->getSettingString('key', $default)
  • $yourModel->getSettingInteger('key', $default)
  • $yourModel->getSettingFloat('key', $default)
  • $yourModel->getSettingBoolean('key', $default)
  • $yourModel->getSettingArray('key', $default)
  • $yourModel->getSettingObject('key', $default)

Delete a setting

$user = App\Models\User::find(1);
$user->deleteSetting('subscribe_newsletter');

Forget a setting (deprecated, use delete instead)

$user = App\Models\User::find(1);
$user->forgetSetting('subscribe_newsletter');

Default Settings

You may want add default settings value for global or model specific settings. You can add these settings value in the configuration file located at configs/laravel-settings.php.

Note: if you cannot find the config file, you need to publish the configuration first.

Default global settings

return [
    // others config

    'defaults' => [
        'site_title' => 'Your Awesome Site',
    ],
];

Now, you will always get the default site title setting if there is no setting stored in the database.

use RuangDeveloper\LaravelSettings\Facades\Settings;


$siteTItle = Settings::get('site_title');

echo $siteTitle; // Your Awesome Site

Default model specific settings

return [
    // others config

    'model_defaults' => [
        'App\Models\User' => [
            'subscribe_newsletter' => false
        ],
    ],
];

Now, you will always get the default subscribe newsletter setting when you try to retrieve it from an user that didn't have setting for subscribe newsletter.

$user = App\Models\User::find(1);
$isSubscribed = $user->getSetting('subscribe_newsletter');

echo $isSubscribed // false

Cache

As you know, this package uses a database to store setting values. Every time you retrieve a setting value, it means you are making a query to the database. This is not a problem for small-scale applications, but it can have a significant impact when used in large-scale applications. To work around this, you can enable caching to store setting values in the cache, so the query is only performed once when you first attempt to retrieve a setting value.

To enable caching, you can go to the file config/laravel-settings.php and change the value of with_cache to true. You can also set the prefix and lifetime there.

<?php

return [
    // Change this to true if you want to use the cache feature.
    'with_cache' => true,

    // The cache key prefix that will be used to store the settings in the cache.
    'cache_prefix' => 'laravel-settings',

    // Cache lifetime in seconds.
    'cache_lifetime' => 60 * 60 * 24 * 7, // 7 days

    // other config
];

Customization

Using Your Own Model

While this package covers almost everything that you need to manage the settings, you may want to use your own model. To do this, you can extends the Setting model from this package.

use RuangDeveloper\LaravelSettings\Models\Setting;

class CustomSetting extends Setting
{
    //
}

After that, you need to tell this package to use the model you just created. Please update in the configs/laravel-settings.php.

return [
    // others config

    'model' => App\Models\CustomSetting::class,
];

Contributing

https://github.com/ruangdeveloper/laravel-settings/blob/main/CONTRIBUTING.md

License

https://github.com/ruangdeveloper/laravel-settings/blob/main/LICENSE.md

ruangdeveloper/laravel-settings 适用场景与选型建议

ruangdeveloper/laravel-settings 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 15.6k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2023 年 10 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 ruangdeveloper/laravel-settings 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ruangdeveloper/laravel-settings 我们能提供哪些服务?
定制开发 / 二次开发

基于 ruangdeveloper/laravel-settings 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-10-24