cmarfil/laravel-multiuser-json-settings

Laravel 4 的简单用户 JSON 设置外观。

v1.1.3 2015-02-20 13:27 UTC

This package is not auto-updated.

Last update: 2024-09-28 17:12:34 UTC


README

为 Laravel 4 提供简单用户设置外观。设置以 JSON 格式存储在单个数据库列中,因此可以轻松地将其添加到现有表(例如 users)。

安装

  1. 首先通过 Composer 安装此包。编辑您的项目 composer.json 文件,以要求 cmarfil/laravel-multiuser-json-settings。
    "require": {
    	"cmarfil/laravel-multiuser-json-settings": "~1.1"
    }
    
  2. 'Cmarfil\LaravelMultiUserJsonSettings\ServiceProvider' 添加到 app/config/app.php 中的 providers
'providers' => array(
  // ...
  'Cmarfil\LaravelMultiUserJsonSettings\ServiceProvider',
),
  1. 'Setting' => 'Cmarfil\LaravelMultiUserJsonSettings\Facade' 添加到 app/config/app.php 中的 aliases
'aliases' => array(
  // ...
  'Setting' => 'Cmarfil\LaravelMultiUserJsonSettings\Facade',
),
  1. 运行 php artisan config:publish cmarfil/laravel-multiuser-json-settings 以发布配置文件。
  2. 修改位于 app/config/packages/cmarfil/laravel-multiuser-json-settings/config.php 的已发布配置文件,以满足您的需求。
  3. 在您的数据库表中创建一个 varchar(字符串)列,以匹配步骤 5 中的配置文件。或者,使用本包中包含的 Laravel 迁移自动在 users 表中创建 settings 列:php artisan migrate --package=cmarfil/laravel-multiuser-json-settings

配置

打开 app/config/packages/cmarfil/laravel-multiuser-json-settings/config.php 以调整包配置。如果此文件不存在,请运行 php artisan config:publish cmarfil/laravel-multiuser-json-settings 以创建默认配置文件。

return array(
    'table' => 'users',
    'column' => 'settings',
	'constraint_key' => 'id',
	'default_constraint_value' => (Auth::check() ? Auth::id() : null)
	'custom_constraint' => false, //'id = ' . (Auth::check() ? Auth::id() : null),
);

指定您要使用的数据库表。

指定要存储设置 JSON 数据的上述表中的列。

约束键

指定列约束 - 这用于区分不同的用户、对象或模型(通常是 id)。

默认约束值

指定默认约束值 - 如果您未指定,则获取默认配置,在这种情况下是登录的用户。

自定义约束

指定每个查询的 where 子句 - 注意:如果要设置或获取同一运行时的不同行,则留空,使用 constraint_key 和 default_constraint_value

用法

使用 Setting 外观(Setting::)来访问此包中的功能。

设置

Setting::set('key', 'value', $constraint_value);

使用 set 来更改设置的值。如果设置不存在,则会自动创建。您可以通过将关联数组(key=>value)传递给第一个参数来一次设置多个键。如果不传递 constraint_value,则默认使用 default_constraint_value。

获取

Setting::get('key', 'default', $constraint_value);

使用 get 来检索设置的值。第二个参数是可选的,可以用来指定设置不存在时的默认值(默认默认值是 null)。如果不传递 constraint_value,则默认使用 default_constraint_value。

忘记

Setting::forget('key', $constraint_value);

通过调用 forget 来取消设置或删除设置。如果不传递 constraint_value,则默认使用 default_constraint_value。

Setting::has('key', $constraint_value);

检查设置的是否存在,返回布尔值。如果不传递 constraint_value,则默认使用 default_constraint_value。

所有

Setting::all($constraint_value);

检索所有设置作为关联数组(key=>value)。如果不传递 constraint_value,则默认使用 default_constraint_value。

保存

Setting::save($constraint_value);

将所有更改保存回数据库。在做出更改后需要调用此操作;此操作不是自动执行的。如果不传递 constraint_value,则默认使用 default_constraint_value 的值

加载

Setting::load($constraint_value);

从数据库重新加载设置。如果在访问或修改设置之前尚未加载设置,则会自动调用此操作。如果不传递 constraint_value,则默认使用 default_constraint_value 的值

## 使用默认配置的示例

return array(
    'table' => 'users',
    'column' => 'settings',
	'constraint_key' => 'id',
	'default_constraint_value' => (Auth::check() ? Auth::id() : null)
	'custom_constraint' => false, //'id = ' . (Auth::check() ? Auth::id() : null),
);

以下操作设置并返回“已登录用户”的设置 "email_notification"

//Set email_notifications setting to false
Setting::set('email_notifications', false);

//Save config
Setting::save();

//Save email_notifications
return Setting::get('email_notifications');

以下操作设置并返回 用户ID为23的用户 的设置 "email_notification"

//Set email_notifications setting to false
Setting::set('email_notifications', false, 23);

//Save config
Setting::save(23);

//Save email_notifications
return Setting::get('email_notifications', true, 23);

最后

贡献

如果您想贡献,请随意创建一个分支并提交一个拉取请求。

错误报告

如果您发现有问题,请在 GitHub 上提出问题。

鸣谢

基于 https://github.com/Grimthorr/laravel-user-settings 的分支。部分基于 https://github.com/anlutro/laravel-settings 开发。