remzikocak/laravel-options
Composer 安装命令:
composer require remzikocak/laravel-options
包简介
Database Options/Settings Package for Laravel 9/10/11/12.
README 文档
README
Laravel Options Package
This Package will help you to dynamically add Option fields to your Backend Panel.
Installation
You can install the package via composer:
composer require remzikocak/laravel-options
The Package will automatically register the Service Provider and Facade. Afterwards, you need to publish and migrate:
php artisan vendor:publish --provider="RKocak\Options\OptionsServiceProvider"
php artisan migrate
This will create the migration files and options.php file in your config folder.
Usage
First of all, you need to create a Optiongroup and an Option.
use RKocak\Options\Models\Optiongroup; use RKocak\Options\Models\Option; Optiongroup::create([ 'label' => 'My Optiongroup', 'description' => 'Optiongroup description', // can be null 'display_order' => 1, ]); Option::create([ 'name' => 'myOption' // should be unique 'label' => 'My Option', 'description' => 'My awesome Option', 'value' => 'Option value', 'type' => 'text', ]);
Option groups and options have a many-to-many relationship. Groups can have many options, and options can belong to many groups. Understanding this relationship is essential for displaying them in your backend panel.
For assignment, use the following:
$group = Optiongroup::find(1); $option = Option::find(1); // Assign option to a group $group->options()->attach($option); // or.. $option->groups()->attach($group);
Getting options
To retrieve the computed value, use the getValue() method from the Option Model.
$option->getValue(); // This will be the "raw" value that is stored in the database $option->value
A better and more performant option is to utilize the Options Facade.
This will cache the options when you only require a key => value store.
You can use it as following:
Options::get('optionName'); // You can pass a second parameter as default value Options::get('optionName', null); // or use the helper function options('optionName', null);
Check if option with the given name exists
Options::has('optionName');
While the cache usually refreshes automatically, if you need to do it manually, use the following method.
Options::getLoader()->rebuildCache();
Adding custom Types
To add your custom type, you need to create a class that extends RKocak\Options\Type
Example:
<?php namespace App; use RKocak\Options\Models\Option; use RKocak\Options\Type; class MyType extends Type { /** * @return string */ public static function getName(): string { return 'my_type'; } /** * @param Option $option * @return string */ public function render(Option $option): string { return '<div> <input type="text" name="options['. htmlspecialchars($option->name) .']" id="options['. htmlspecialchars($option->name) .']" value="'. htmlspecialchars($option->getValue()) .'" class=""/> </div>'; } /** * Store the new value * * @param $newValue * @param $oldValue * @return mixed */ public function store($newValue, $oldValue) { return $newValue; } /** * Cast value * * @param $value * @return mixed|string */ public function cast($value) { return (string) $value; } }
then add the class in configuration file to the types array. After that you can use it like all other types.
Rendering HTML for Edit Form
Rendering options is straightforward with a few pre-added "Types."
$option = Option::first(); $html = $option->renderEditHTML(); // ..or $html = $option->toHtml(); // Alternatively, you can use the model instance directly in your Blade views: {{ $option }}
License
The MIT License (MIT). Please see License File for more information.
remzikocak/laravel-options 适用场景与选型建议
remzikocak/laravel-options 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.18k 次下载、GitHub Stars 达 8, 最近一次更新时间为 2020 年 04 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 remzikocak/laravel-options 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 remzikocak/laravel-options 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 3.18k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-04-23
