marceauka / laravel-crudable
由自定义字段驱动的 Laravel CRUD 构建器
Requires
- php: >=5.6.4
- laravel/framework: >=5.3
Requires (Dev)
- laracasts/testdummy: ~2.0
- laravel/laravel: ~5.4
- mockery/mockery: ~0.9
- phpspec/phpspec: ~2.1
- phpunit/phpunit: ~5.0
This package is auto-updated.
Last update: 2020-03-17 17:23:05 UTC
README
Laravel Crudable 是一个库,旨在将 自定义字段 驱动的 CRUD 功能 带到您的 Eloquent 模型。
摘要
初学者的逐步教程在此: 入门指南(也提供 3 分钟 视频)。
目标
- 易于集成到 新项目
- 易于集成到 现有项目
- 非侵入式 API(只需向您的模型添加一个特质和一个方法)
- 专注于字段
- 可定制
- 遵循 Laravel 的方式
非目标
- 角色或权限
- 管理面板
安装
此版本与 Laravel 5.4 和 5.3 兼容。有关 Laravel 5.2 兼容性,请查看分支 1.0。
通过 composer 安装
composer require marceauka/laravel-crudable
然后,在您的 config/app.php
中注册服务提供者。
// After other service providers Akibatech\Crud\CrudServiceProvider::class
最后,发布资源
php artisan vendor:publish --tag=crud
此命令将发布语言文件和视图,以便于自定义。
使用
将特质 Crudable
添加到您的 Eloquent 模型,然后实现所需的方法 getCrudFields
示例模型
class Post extends Model { use Crudable; /** * @return \Akibatech\Crud\Services\CrudFields */ public function getCrudFields() { return CrudFields::make([ // Bind the title attribute to a required Text Field. TextField::handle('title', 'required|min:3')->withPlaceholder('Title of the post'), // Bind the introduction attribute to a required Textarea Field. TextareaField::handle('introduction', 'required|min:3')->withPlaceholder('Short introduction'), // Bind the content attribute to a Tinymce Field TinymceField::handle('content', 'required'), // Bind the illustration attribute to a file upload allowing 10Mb JPG or PNG picture FileUploadField::handle('illustration')->withMaxSize(1024 * 1024)->withTypes('jpeg,png'), // Bind the status attribute to a Radio Field allowing Draft or Live options. RadioField::handle('status', 'required')->withOptions([0 => 'Draft', 1 => 'Live']) ]); } }
显示条目表
在您的控制器中
public function index() { $model = App\Post::class; return view('your-view', compact($model)); }
在您的视图中
@crudtable($model)
了解更多: 表格
显示条目创建表单
在您的控制器中
public function create() { $model = App\Post::class; return view('your-view', compact($model)); } public function store(Request $request) { $entry = (new App\Post)->crudEntry(); $validation = $entry->validate($request->all()); if ($validation->passes()) { $validation->save(); } // Redirect to the form with the errors if validation fails, or to the index page return $validation->redirect(); }
在您的视图中
@crudentry($model)
了解更多: 条目
字段
字段是将您的 模型属性 绑定到 强大的行为 和 可重用视图组件 的方式。
在此阶段,您可以使用 TextField
、TextareaField
、RadioField
、EmailField
、TinymceField
、FileUploadField
、SelectRelationField
和 DatePickerField
,但还有许多其他计划。
了解更多: 字段
控制器和路由
默认情况下,每个被 CRUD 化的模型都需要一个控制器。
您可以使用命令 make:crud:controller <controller-name> <model-name>
来生成它。
例如:artisan make:crud:controller PostsController Post
。
此命令将为您的模型生成一个CRUD控制器,并附带一些基本的视图,但自定义这些视图的工作由您来完成。
生成后,您需要像这样注册路由
// routes/web.php App\Post::crudRoutes();
了解更多: 路由和控制器
自定义
所有视图都可以自定义,并存储在 resources/views/vendor/crud
中。
完整文档: 自定义视图
测试
您可以使用以下命令启动测试
vendor/bin/phpunit
贡献
欢迎使用此仓库中的问题和拉取请求进行贡献。
作者
许可
MIT许可(MIT) 版权(c)2016 Marceau Casals
在此协议下,任何获得此软件及其相关文档副本(“软件”)的个人均可免费使用该软件,不受任何限制,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本的权利,并允许向获得软件的个人提供软件副本,前提是遵守以下条件
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
本软件按“现状”提供,不提供任何形式的保证,明示或暗示,包括但不限于适销性、针对特定目的的适用性和非侵权性。在任何情况下,作者或版权所有者不应对任何索赔、损害或其他责任负责,无论源于合同、侵权或其他方式,与软件或软件的使用或其他交易有关。