toriomlab / eloquent-form-elements
来自 Laravel 的简单 eloquent 模型表单元素生成器包
Requires
- php: >=7.1.0
- illuminate/support: ~5.0
This package is auto-updated.
Last update: 2024-09-27 12:34:46 UTC
README
Eloquent-Fields 生成器包,适用于 Laravel
此包实际上包含一个辅助函数和一个特质。其基本目标是减少开发者编写 Eloquent 模型创建和更新表单标记所花费的时间。
其基本思路是在您的模型中使用 FormGenerator 特质,并编写一个包含字段偏好的小数组。然后您可以使用辅助函数生成创建或更新表单。
安装。
通过 composer
composer require toriomlab/eloquent-form-elements dev-master
然后在您的模型中使用特质 toriomlab\EloquentFormElements\Traits\FormGenerator,如下所示
<?php namespace App; use Illuminate\Database\Eloquent\Model; use ToriomLab\EloquentFormElements\Traits\FormGenerator; class Fee extends Model { use FormGenerator; }
如何使用它?
通过在模型中定义一个名为 $fields 的静态数组,其结构应如下示例。
public static $fields = [ 'field_name_attribute' => [ // Field name attribute should be the column name in DB as well to retreive its value in update form generation. 'label' => 'Field Label Text', // Field Label. 'input' => 'input', // Field Type. 'type' => 'text' // Field Input Type. ], ];
这将生成一个包含此字段的 bootstrap form-group div。然后您可以在视图文件中生成创建表单字段,如下例所示
<form class="form-horizontal" method="post"> {!! generate_fields('App\Fee') !!} {!! csrf_field() !!} <button type="submit" class="btn btn-primary">حفظ!</button> </form>
您可以通过传递包含对象 ID 的第二个参数来创建更新表单字段。
<form class="form-horizontal" method="post"> {!! generate_fields('App\Fee', 1) !!} {!! csrf_field() !!} <button type="submit" class="btn btn-primary">حفظ!</button> </form>
因此,这实际上将生成 ID 为 1 的行的更新表单字段。
完整指南
$fields 数组编写规则。
- 必须提供
label。 - 必须提供
input,可以是input、select或textarea。
如果 input 索引是 input,则必须提供 type 索引以提供输入类型,如 text、number、date 等。
如果 input 类型是 select,您可以提供 options 索引,它将包含一个用于选择下拉选项的数组。或者,您还可以提供 relation 索引,它将从 Eloquent 模型的关联中获取选项。
关系数组
该包目前支持三种 Eloquent 关联。您必须在关系数组中提供 type 索引以指定以下关系类型
belongsTo:其类型将是one,因为我们在这种情况下只会选择一个选项。hasMany或belongsToMany:它们的类型将是many,因为我们将在这些情况下选择多个值。
如果关系数组中的 type 索引是 one,您需要提供
model索引,它将是关联模型。column,它是外键。selectFrom,它将是我们需要从关联模型中显示的列。valueFrom,它将是关联模型中作为选择下拉选项的值的列。
如果关系数组中的 type 索引是 many,您需要提供
name索引,它将是关联的名称。例如,如果我的模型是User并且它有多个Role,则关联的名称将是roles或您在User模型中创建的任何关联名称。
您还需要提供 selectFrom 和 valueFrom。
如果输入是 select,您还可以提供 valueFallback,它将是一个返回下拉中所有选项的静态方法。然后您需要提供一个 valueCallback,它将返回所选选项的集合。
其他偏好设置
在所有字段规范中,有多个可能非常有用的额外事物。
label_classes是标签类属性值。您可以覆盖默认类control-label col-md-4。input_div_classes表示包含输入代码的div的class属性。默认值为col-md-6。input_classes:表示输入本身的class属性。默认值为form-control。input_id表示输入本身的id属性。默认值为字段名。inject_attributes允许您向输入添加额外的属性。它只是将字符串放入输入标签中。
示例。
生成普通文本字段
'name' => [ 'label' => 'Your name', 'input' => 'input', 'type' => 'text' ],
生成数字字段
'age' => [ 'label' => 'Your age', 'input' => 'input', 'type' => 'number' ],
生成HTML图像
'avatar' => [ 'label' => 'Label Name', 'element' => 'img', 'image_classes' => 'img-rounded img-thumbnail img-responsive', 'storage_folder' => 'storage/courses/', ],
生成HTML行
'line' => [ 'element' => 'hr' ],
如果模型属于一个角色,并且我们想为角色创建一个选择字段。
'role_id' => [ 'label' =>'Role', 'input' => 'select', 'relation' => [ 'model' => 'App\Role', 'type' => 'one', 'column' => 'role_id', 'selectFrom' => 'name', 'valueFrom' => 'id', // This is how selectFrom and valueFrom works // <option value="{{ $role->id }}">{{ $role->name }}</option> ], ],
如果模型属于多个角色呢?
'roles[]' => [ 'label' => 'Roles', 'input' => 'select', 'relation' => [ 'name' => 'roles', 'model' => 'App\Role', 'type' => 'many', 'selectFrom' => 'name', 'valueFrom' => 'id' ], 'inject_attributes' => 'multiple' ],
一个完整的示例,展示用户模型的 $fields 数组应该如何。
<?php namespace App; use Illuminate\Database\Eloquent\Model; use ToriomLab\EloquentFormElements\Traits\FormGenerator; class User extends Model { use FormGenerator; public static $fields = [ 'name' => [ 'label' => 'Your name', 'input' => 'input', 'type' => 'text', ], 'age' => [ 'label' => 'Your age', 'input' => 'input', 'type' => 'number', ], 'roles[]' => [ 'label' => 'Roles', 'input' => 'select', 'relation' => [ 'name' => 'roles', 'model' => 'App\Role', 'type' => 'many', 'selectFrom' => 'name', 'valueFrom' => 'id', ], 'inject_attributes' => 'multiple' ], ]; public function roles() { return $this->belongsToMany('App\Role'); } }
valueCallbacks 和 valueFallbacks 的示例。
public static $fields = [ 'exception_ids[]' => [ 'label' => 'المنتجات المستثناة', 'input' => 'select', 'valueCallback' => 'getExceptionsValues', 'valueFallback' => 'getAllProducts', 'selectFrom' => 'name_ar', 'valueFrom' => 'id', 'inject_attributes' => 'multiple' ], ]; public function getExceptionsValues() { $results = []; $exception_ids = $this->exception_ids ? json_decode($this->exception_ids) : []; foreach ($exception_ids as $exception_id) { $product = Product::find($exception_id); $results[] = $product; } return $results; } public static function getAllProducts() { return Product::latest()->get(); }
使用 createValueCallbacks 和 updateValueFallbacks 的手动 belongsTo 关系的示例。
/** * The attributes that are building the model forms. * * @var array */ public static $fields = [ 'name' => [ 'label' => 'Category Name', ], 'category_id' => [ 'label' => 'Categories', 'input' => 'select', 'options' => [ '' => 'All Categories', ], 'selectFrom' => 'name', 'valueFrom' => 'id', 'valueCallback' => 'getCurrentValue', // Can be replaced with 'column' => 'category_id' for belongsTo relation 'updateValueFallback' => 'getUpdateCategories', 'createValueFallback' => 'getAllCategories', ], ]; /** * Get dropdown categories for EloquentFormElements. * * @return self */ public function getUpdateCategories() { return static::where('id', '!=', $this->id)->get(); } /** * Get all dropdown categories for EloquentFormElements Creation. * * @return self */ public static function getAllCategories() { return static::latest()->get(); } /** * Get Current value for the manual relation. * @return mixed */ public function getCurrentValue() { return $this->category_id; }
然后,您可以调用 generate_fields('App\User') 生成创建表单字段,或者调用 generate_fields('App\User', 1) 为ID为1的用户生成更新表单字段。
注意,您可以向 generate_fields 参数传递第三个参数,以从表单中排除一个或多个字段。
因此,如果我们调用 generate_fields('App\User', null, 'age'),则创建的表单将没有 age 字段。您也可以传递一个数组,如 generate_fields('App\User', null, ['age', 'roles']) 将创建一个没有角色和年龄字段的创建表单。