informatica_mobius / laravel-custom-fields
Laravel Custom Fields 是一个允许您为任何 Laravel 模型添加自定义字段并将对这些字段的响应存储在任何 Laravel 模型上的包。
Requires
- php: ^8.1
- illuminate/support: ^9.0|^10.0|^11.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.6
- orchestra/testbench: ^7.6|^8.0
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-10-03 12:29:27 UTC
README
Laravel Custom Fields 是一个允许您为任何 Laravel11 模型添加自定义字段并将这些字段的响应与其他模型关联的包。
安装
要开始使用,将 informatica_mobius/laravel-custom-fields
包添加到您的 composer.json
文件中并更新您的依赖项
composer require informatica_mobius/laravel-custom-fields
发布迁移
php artisan vendor:publish --provider="InformaticaMobius\LaravelCustomFields\LaravelCustomFieldsServiceProvider" --tag="migrations"
运行迁移
php artisan migrate
您可以使用配置选项自定义表名。更多内容将在后面介绍。
示例 - 问卷调查应用
为了说明文档,让我们使用一个问卷调查构建应用的例子。管理员可能会使用后端创建包含问题的 Surveys
,然后最终用户填写这些调查,生成 SurveyResponses
。
准备您的模型
添加自定义字段
要添加基本的自定义字段支持,只需在您的模型顶部添加 HasCustomFields
特性
use Illuminate\Database\Eloquent\Model; use InformaticaMobius\LaravelCustomFields\Traits\HasCustomFields; class Survey extends Model { use HasCustomFields; // ... }
添加自定义字段响应
接下来,我们添加支持存储自定义字段响应。我们将在 SurveyResponse
模型顶部添加 HasCustomFieldResponses
特性。
use Illuminate\Database\Eloquent\Model; use InformaticaMobius\LaravelCustomFields\Traits\HasCustomFieldResponses; class SurveyResponse extends Model { use HasCustomFieldResponses; // ... }
基本用法
创建字段
您可以将字段添加到模型,如下所示
$survey->customFields()->create([ 'title' => 'What is your name?', 'type' => 'text' ]);
每个字段可以包含以下内容:(更多内容将在后面介绍)
title
: 自定义字段的标题/问题。 description
: 字段的描述。对于提供更多上下文给填写字段的用户很有用。 type
: 您要创建的字段类型。可用的类型将在下一节中概述。 required
: 表示字段是否必需的布尔值。 answers
: 对于具有用户选择的字段,可以接受值的数组。
创建字段响应
要存储字段上的响应,您可以这样做
$field->responses()->create([ 'value' => 'John Doe' ]);
检索字段
要检索自定义字段,请使用 customFields
关系
$survey->customFields()->get();
检索字段响应
要检索字段上的响应,请使用 responses()
关系
$field->responses()->get();
自定义字段类型
自定义字段可以是以下5种类型之一
text
: 自由输入字段,存储为字符串。用于简单的输入,最大长度为255个字符。textarea
: 自由输入字段,存储为文本列。用于可能不适合text
字段的长文本。radio
: 这些是多选字段,需要您传递answers
属性。select
: 这些是多选字段,需要您传递answers
属性。checkbox
: 布尔字段。
通常,这些字段类型与它们对应的 HTML 元素相对应。将来我们可能会提供这些字段的客户端脚手架,但现在这取决于您。
*radio
和 select
字段类型需要您在字段上填写 answers
属性。这是一个简单的字符串数组,是该字段的有效响应。例如
$survey->customFields()->create([ 'title' => 'What is your favorite color?', 'type' => 'select', 'answers' => ['Red', 'Green', 'Blue', 'Yellow'], ]);
验证响应
验证助手
在大多数情况下,您在保存之前需要验证自定义字段的响应。您可以通过在您的模型上调用 validateCustomFields()
函数来实现。
$responses = [ '1' => "John Doe", '2' => "Blue" ]; $survey->validateCustomFields($responses);
您还可以传递一个 Request
对象。
use Request; class FooController extends Controller { public function index(Request $request) { $validation = $survey->validateCustomFields($request); // ... } }
<form> <input type="text" name="custom_fields[]" /> </form>
当使用 Request
对象时,输入键应该是值的数组。
隐式验证规则
上面描述的5种支持的字段类型自动应用以下验证规则:
text
:string|max:255
textarea
:string
radio
:string|max:255|in:answers
select
:string|max:255|in:answers
checkbox
:in:0,1
重要:当使用复选框时,请确保将未选中的复选框也作为POST发送,否则您的响应数据可能不完整。
必填字段
由于它们很常见,因此此包原生支持必填字段。要标记字段为必填,只需在创建自定义字段时将 required
设置为 true。
$survey->customFields()->create([ 'title' => 'Do you love Laravel?', 'type' => 'radio', 'answers' => ['Yes', 'YES'], 'required' => true ]);
自定义验证规则
除了内置验证规则外,您还可以将自定义规则应用于任何自定义字段。例如,如果您想验证字段是一个介于1和10之间的整数,您可以这样做:
$survey->customFields()->create([ 'title' => 'Pick a number 1-10', 'type' => 'text', 'validation_rules' => 'integer|min:1|max:10' ]);
记住,validation_rules
支持Laravel中可用的任何 验证规则。
验证规则集
-> nah? 在某些情况下,定义验证规则集更容易、更实用。例如,在我们的调查应用程序中,如果我们想提供
保存响应
要存储自定义字段的响应,只需调用 saveCustomFields()
并传入一个值数组。
saveCustomFields
函数可以接受一个请求或数组。
$surveyResponse->saveCustomFields([' ']);
如果您正在提交表单请求,您可以轻松地
Use App\... $surveyResponse->saveCustomFields($request->input);
查询响应
您可以使用 WhereCustomField()
范围在任意字段上查询响应。该函数接受字段对象和您要查找的值。有关查询范围更多信息,请访问 此链接。
例如,如果您想找到所有带有 large
T恤尺码的 SurveyResponses
,请执行以下查询:
Use App\Models\SurveyResponse; Use App\Models\SurveyResponse; $field = SurveyResponse::WhereCustomField($field, 'large')->get();
排序
您可以通过使用 order
函数更改模型上自定义字段的顺序。传入一个数组或 Collection
的id。字段的索引位置表示其顺序位置。
$survey->orderCustomFields([2, 4, 5]); // Field with id 2 will be ordered first.
您还可以手动更改 order
列的值
$field->order = 3; $field->save();
默认情况下,通过关系检索时,自定义字段以升序返回
$survey->customFields()->get(); // Returned in ascending order.
配置
要发布配置文件,请运行以下命令
php artisan vendor:publish --provider="InformaticaMobius\LaravelCustomFields\LaravelCustomFieldsServiceProvider" --tag="config"
配置文件现在应发布在 config/custom-fields.php
中。可用选项及其用法在发布的文件中有解释。