定制 stechstudio/laravel-settings 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

stechstudio/laravel-settings

Composer 安装命令:

composer require stechstudio/laravel-settings

包简介

Polymorphic, typed key/value settings for any Eloquent model.

README 文档

README

Polymorphic, typed key/value settings for any Eloquent model.

Drop the HasSettings trait onto any model and instantly get a bag of settings stored in a single polymorphic table. Values keep their PHP type — booleans come back as booleans, arrays as arrays, integers as integers — without you having to think about casting.

$user->setSetting('theme', 'dark');
$user->setSetting('notifications', true);
$user->setSetting('limits', ['daily' => 100, 'monthly' => 2000]);

$user->setting('theme');          // "dark"
$user->setting('notifications');  // true  (bool)
$user->setting('limits');         // ['daily' => 100, 'monthly' => 2000]
$user->setting('missing', 'n/a'); // "n/a" (default)

Installation

composer require stechstudio/laravel-settings

Publish and run the migration:

php artisan vendor:publish --tag=settings-migrations
php artisan migrate

The package auto-registers its service provider. The migration is also loaded automatically, so in development you can migrate without publishing — publish it when you want the file committed to your app.

Optionally publish the config:

php artisan vendor:publish --tag=settings-config

Usage

Add the trait to any model:

use STS\Settings\Concerns\HasSettings;

class User extends Authenticatable
{
    use HasSettings;
}

Reading & writing

// Write — the cast type is auto-detected from the value
$user->setSetting('theme', 'dark');

// Read, with an optional default
$user->setting('theme');             // "dark"
$user->setting('missing', 'default'); // "default"

// Existence
$user->hasSetting('theme');          // true

// Delete
$user->deleteSetting('theme');       // true

// Everything as a name => value map
$user->allSettings();                // ['theme' => 'dark', ...]

// Increment a numeric setting (creates it at $amount if absent)
$user->incrementSetting('login_count');     // +1
$user->incrementSetting('credits', 25);     // +25

Type casting

Types are detected automatically on write (bool, int, float, array, string). Pass an explicit cast as the third argument when you need to override detection — for example to store a numeric string as a string:

use STS\Settings\Models\Setting;

$user->setSetting('zip', '90210', Setting::CAST_STRING);
$user->setting('zip'); // "90210" (stays a string)

Available casts: Setting::CAST_STRING, CAST_INT, CAST_FLOAT, CAST_BOOL, CAST_ARRAY, CAST_JSON.

The relationship

HasSettings adds a standard settings() morph-many relationship, so you can eager load and query it like any other:

$users = User::with('settings')->get();

Settings are scoped to their owner — each model only ever sees its own.

Configuration

config/settings.php:

return [
    // The table that holds settings rows.
    'table' => 'settings',

    // The model representing a single setting. Point this at your own
    // subclass of STS\Settings\Models\Setting to add scopes or behaviour.
    'model' => STS\Settings\Models\Setting::class,
];

Testing

composer test

License

MIT. See LICENSE.md.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-06-27

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固