signifly / laravel-configurable
使您的 Eloquent 模型可配置。
v1.5.0
2021-03-29 08:29 UTC
Requires
- php: ^7.3|^8.0
- illuminate/database: ^6.0|^7.0|^8.0
- illuminate/support: ^6.0|^7.0|^8.0
Requires (Dev)
- mockery/mockery: ^1.2
- orchestra/testbench: ^4.0|^5.0|^6.0
- phpunit/phpunit: ^7.0|^8.0|^9.0
README
signifly/laravel-configurable
包允许您轻松地使您的 Eloquent 模型可配置。
以下是如何使用它的一个小示例。
// Remember to add use statement use Signifly\Configurable\Configurable; class User { use Configurable; // Remember to make `config` fillable protected $fillable = [ 'config', ]; // Remember to add `config` to casts protected $casts = [ 'config' => 'array', ]; }
将列添加到您的表迁移中
Schema::table('users', function (Blueprint $table) { $table->json('config')->nullable(); });
现在您就可以配置您的用户模型了
$user = User::find(1); $user->config()->some_key = 'some val'; $user->config()->set('some_other_key', 'some other val'); $user->save();
从配置中检索数据很简单
$user = User::find(1); $user->config()->some_key; // returns some val $user->config()->get('some_other_key'); // return some other val
从配置中删除属性可以这样做
$user = User::find(1); $user->config()->remove('some_key'); $user->save();
检查属性是否存在于配置中
$user = User::find(1); $user->config()->has('some_key'); // returns true
以集合的形式检索属性
$user = User::find(1); $user->config()->collect('some_key'); // returns Collection(['some val']);
您也可以覆盖配置键
// Remember to add use statement use Signifly\Configurable\Configurable; class User { use Configurable; // Remember to make `settings` fillable protected $fillable = [ 'settings', 'extras', ]; // Remember to add `settings` to casts protected $casts = [ 'settings' => 'array', 'extras' => 'array', ]; protected function getConfigKey() { return 'settings'; } // or add a custom config attribute like this: public function getExtrasAttribute() { return new Config($this, 'extras'); } }
文档
在提供更多文档之前,请查阅测试用例。
安装
您可以通过 composer 安装此包
$ composer require signifly/laravel-configurable
该包将自动注册自己。
测试
$ composer test
安全性
如果您发现任何安全问题,请通过电子邮件 dev@signifly.com 报告,而不是使用问题跟踪器。
致谢
许可
MIT 许可证 (MIT)。请参阅 许可文件 了解更多信息。