toriomlab/eloquent-form-elements
Composer 安装命令:
composer require toriomlab/eloquent-form-elements
包简介
Simple eloquent model form elements generator package from Laravel
README 文档
README
Eloquent-Fields Generator Package For Laravel
This package actually contains a helper function and a trait. Its basic target is to reduce time that developers spend writing Creation & Update forms markup for Eloquent Models.
It's basic idea is to use the FormGenerator trait in your model and write a tiny array with your fields' preferences. Then you can use the helper function to generate a creation or update form.
Installing.
Via composer
composer require toriomlab/eloquent-form-elements dev-master
Then in your model use the trait toriomlab\EloquentFormElements\Traits\FormGenerator like the following:
<?php namespace App; use Illuminate\Database\Eloquent\Model; use ToriomLab\EloquentFormElements\Traits\FormGenerator; class Fee extends Model { use FormGenerator; }
How to use it?
By defining a static array in your model called $fields and its structure should be as following example.
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. ], ];
This will generate a bootstrap form-group div contains this field.
Then you can generate a creation form fields in your view files like the following example:
<form class="form-horizontal" method="post"> {!! generate_fields('App\Fee') !!} {!! csrf_field() !!} <button type="submit" class="btn btn-primary">حفظ!</button> </form>
And you can create an update form fields by passing a second parameter contains the object id.
<form class="form-horizontal" method="post"> {!! generate_fields('App\Fee', 1) !!} {!! csrf_field() !!} <button type="submit" class="btn btn-primary">حفظ!</button> </form>
So this actually will generate an update form fields for the row which its id is 1.
Full Guide
$fields array writing rules.
- Must provide
label. - Must provide
inputwhich can beinput,selectortextarea.
If the input index is input you must provide a type index to provide the input type like text or number or date etc.
If the input type is select you can provide an options index which will contain an array for the select dropdown options. Or you can provide a relation index which will get options from a relation in the eloquent model.
Relation array
There's three types of Eloquent Relatioships supported till now in the package. You have to provide a type index in the relation array to specify the relation type based on the following:
belongsToits type will beonebecause we will only select one option in this case.hasManyOrbelongsToMany: their type will bemanybecause we will select multiple values.
If the type index in the relation array is one you will need to provide
modelindex which will be the relation model.columnwhich is the foreign key.selectFromwhich will be the column we need to display in the option from the relation model.valueFromwhich will be the column from the relational model that will be the value of the option of the select dropdown.
If the type index in the relation array is many you will need to provide
nameindex which will be the name of the relation. For example if my model isUserand it has manyRoleso the name of the relation will berolesor whatever the name of the relation you created in theUsermodel.
You will also need to provide a selectFrom and valueFrom as well.
If the input is a select you can provide a valueFallback which will be a static method returns all the options in the dropdown and then you need to provide a valueCallback which will return the selected options collection.
Additional Preferences
There are multiple additional things that can be very usefull in all your fields specifications.
label_classeswhich is the label class attribute value. You can override the default classes which arecontrol-label col-md-4.input_div_classeswhich is the class attribute of the div that contains the input code. By default it'scol-md-6.input_classes:which is the class attribute of the input itself. By default it'sform-control.input_idwhich is the id attribute of the input itself. By default it's the field name.inject_attributeswhich allows you to add additional attributes to the input. It just takes the string and put it in the input tag.
Examples.
Generate normal text field:
'name' => [ 'label' => 'Your name', 'input' => 'input', 'type' => 'text' ],
Generate number field:
'age' => [ 'label' => 'Your age', 'input' => 'input', 'type' => 'number' ],
Generate HTML Image:
'avatar' => [ 'label' => 'Label Name', 'element' => 'img', 'image_classes' => 'img-rounded img-thumbnail img-responsive', 'storage_folder' => 'storage/courses/', ],
Generate HTML Line:
'line' => [ 'element' => 'hr' ],
If the model belongs to a role and we wanna make a select field for the roles.
'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> ], ],
What if the model belongs to many roles?
'roles[]' => [ 'label' => 'Roles', 'input' => 'select', 'relation' => [ 'name' => 'roles', 'model' => 'App\Role', 'type' => 'many', 'selectFrom' => 'name', 'valueFrom' => 'id' ], 'inject_attributes' => 'multiple' ],
A full example how should be the $fields array of a user model.
<?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'); } }
Example for valueCallbacks and 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(); }
Example for manual belongsTo relation with createValueCallbacks and updateValueFallbacks.
/** * 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; }
Then you can just call generate_fields('App\User') to generate a creation form fields or generate_fields('App\User', 1) to generate an update form fields for User whose id is 1.
Notice that you can pass a third parameter to generate_fields parameter to except one or many fields from being generated in the form.
So if we called generate_fields('App\User', null, 'age') a creation form will be created without age field. You can also pass an array so generate_fields('App\User', null, ['age', 'roles']) will create a creation form without roles and age fields.
Don't hesitate to make a pull request or post an issue if found.
This package is cloned from lilessam/eloquent-fields
toriomlab/eloquent-form-elements 适用场景与选型建议
toriomlab/eloquent-form-elements 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 139 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 04 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「form generator」 「Eloquent Form Generator」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 toriomlab/eloquent-form-elements 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 toriomlab/eloquent-form-elements 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 toriomlab/eloquent-form-elements 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Memio's PrettyPrinter, used to generate PHP code from given Model
Diese Contao 4 Erweiterung stellt Google reCAPTCHA V2 in Form eines neuen Formularfeldes im Formulargenerator bereit. This extension provides Google reCAPTCHA V2 in the form of a new form field in the form generator of Contao Open Source CMS.
A Laravel Filament Forms slug field.
A jQuery augmented PHP library for creating and validating HTML forms
Laravel contact us form package to send email and save to database
Alfabank REST API integration
统计信息
- 总下载量: 139
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-04-21