atcliff/laravel-crud-forms
Composer 安装命令:
composer require atcliff/laravel-crud-forms
包简介
Create CRUD Forms for Laravel Models.
README 文档
README
This is a Laravel >=5.5 package to help easily create CRUD (Create, Read, Update, Delete) forms for eloquent models (as well as an index page). It aims to be used as a quick tool which does not interfere with the other parts of the application that it's used in.
The package provides:
- A trait to use in resource controllers and
- A series of views for displaying the forms
The views are built using bootstrap (v3), but the styling can easily be overriden.
Installation
Composer
From the command line, run:
composer require achillesp/laravel-crud-forms
Configuration
This package uses a config file which you can override by publishing it to your app's config dir.
php artisan vendor:publish --provider=Achillesp\CrudForms\CrudFormsServiceProvider --tag=config
Usage
To use the package, you need to use the trait Achillesp\CrudForms\CrudForms in your model's controller and define your routes.
The trait provides all the required methods for a Resource Controller, as well as a restore method in case of soft-deleted models.
Routes
If for example you have a Post model, you would define the routes:
Route::resource('/posts', 'PostController');
Controller
Then in your PostController, you will need to use the trait and also define a constructor where you give the needed details of the model.
use App\Post; use Achillesp\CrudForms\CrudForms; class PostController extends Controller { use CrudForms; public function __construct(Post $post) { $this->model = $post; } }
In the controller's constructor you can define the properties which are handled by the controller. The available properties that can be defined are as follows.
The model
This is the model, which should be passed in the constructor through Dependency Injection.
The formFields array
This is an array of all the fields you need in the forms. Each field is declared as an array that has:
name: This is the model's attribute name, as it is in the database.label: This is the field's label in the forms.type: The type of the form input field that will be used. Accepted types are:- text
- textarea
- url
- password
- date
- select
- select_multiple
- checkbox
- checkbox_multiple
- radio
relationship: This is needed in case of a select, select_multiple, radio or checkbox_multiple buttons. You can state here the name of the relationship as it is defined in the model. In the example bellow, thePostmodel has abelongsTorelationship tocategoryand abelongsToManyrelationship totags. ForbelongsTorelationships you can use a select or a radio(group of radios) input. ForbelongsToManyrelationships you can use a select_multiple or checkbox_multiple inputs.relFieldName: This is optional. It is used only in case we have a relationship, to set the name of the attribute of the related model that is displayed (ie. in a select's options). If not defined, the default attribute to be used isname.
$this->formFields = [ ['name' => 'title', 'label' => 'Title', 'type' => 'text'], ['name' => 'slug', 'label' => 'Slug', 'type' => 'text'], ['name' => 'body', 'label' => 'Enter your content here', 'type' => 'textarea'], ['name' => 'publish_on', 'label' => 'Publish Date', 'type' => 'date'], ['name' => 'published', 'label' => 'Published', 'type' => 'checkbox'], ['name' => 'category_id', 'label' => 'Category', 'type' => 'select', 'relationship' => 'category'], ['name' => 'tags', 'label' => 'Tags', 'type' => 'select_multiple', 'relationship' => 'tags'], ];
The indexFields array
These are the model's attributes that are displayed in the index page.
$this->indexFields = ['title', 'category_id', 'published'];
If not defined, then the first of the formFields is shown.
The formTitle (optional)
You can optionally, define the name of the model as we want it to appear in the views. If not defined, the name of the model will be used.
The bladeLayout (optional)
This is used to define the blade layout file that will be extended by the views for the crud forms and index page.
The option to display deleted models (withTrashed)
Setting this to true, will also display deleted models and offer an option to restore them.
$this->withTrashed = true;
In order to be able to restore the models, you need to define an additional route:
Route::put('/posts/{post}/restore', ['as' => 'posts.restore', 'uses' => 'PostController@restore']);
The validationRules array (optional)
These are the rules we want to use to validate data before saving the model.
$this->validationRules = [ 'title' => 'required|max:255', 'slug' => 'required|max:100', 'body' => 'required', 'publish_on' => 'date', 'published' => 'boolean', 'category_id' => 'required|int', ];
The validationMessages array (optional)
Use this to define custom messages for validation errors. For example:
$this->validationMessages = [ 'body.required' => "You need to fill in the post content." ];
The validationAttributes array (optional)
Use this to change the way an attribute's name should appear in validation error messages.
$this->validationAttributes = [ 'title' => 'Post title' ];
Views
The views are built with bootstrap v.3 and also have css classes to support some common JavaScript libraries.
- select2 class is used in select inputs
- datepicker class is used in date inputs
- data-table class is used in the index view table
It is also possible to publish the views, so you can change them anyway you need. To publish them, use the following artisan command:
php artisan vendor:publish --provider=Achillesp\CrudForms\CrudFormsServiceProvider --tag=views
License
The MIT License (MIT). Please see License File for more information.
atcliff/laravel-crud-forms 适用场景与选型建议
atcliff/laravel-crud-forms 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.89k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 10 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「Forms」 「crud」 「laravel」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 atcliff/laravel-crud-forms 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 atcliff/laravel-crud-forms 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 atcliff/laravel-crud-forms 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
ConfirmationField is a form field for Atk14 applications. It's like the BooleanField (checkbox) but the ConfirmationField must be ticked.
Gii Generator for double model generation
Field for number with restricted count of digits and decimal places
A PSR-7 compatible library for making CRUD API endpoints
HTML and form generation
Build forms from schema
统计信息
- 总下载量: 9.89k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-10-25