netcore / module-crud
此包的最新版本(v1.0.6)没有可用的许可证信息。
v1.0.6
2018-10-11 12:02 UTC
Requires
- php: >=7.0
- maatwebsite/excel: ~2.1.0
This package is not auto-updated.
Last update: 2024-09-26 15:46:16 UTC
README
安装前
此包是Netcore CMS生态系统的一部分,并且仅在安装了以下包的项目中才能正常工作:
- https://github.com/netcore/netcore
- https://github.com/netcore/module-admin
- https://github.com/nWidart/laravel-modules
安装
composer require netcore/module-crud
您需要将CRUDModel
和CRUDController
特质添加到您的模型和控制器中。
控制器
<?php namespace App\Http\Controllers; use App\Article; use Modules\Crud\Traits\CRUDController; class ArticlesController extends Controller { use CRUDController; // <- This trait is required. /** * CRUD model. * * @var \Illuminate\Database\Eloquent\Model */ protected $model; /** * ArticlesController constructor. */ public function __construct() { $this->model = app(Article::class); // <- Set model. } }
模型
<?php namespace App; use Illuminate\Database\Eloquent\Model; use Modules\Crud\Traits\CRUDModel; class Article extends Model { use CRUDModel; // <- This is required /** * Mass assignable fields. * * @var array **/ protected $fillable = [ 'is_published', 'views', ]; .... Relations etc ... }
数据表配置
默认情况下,数据表列与可批量分配的字段相等,但您可以轻松地配置一切。
首先,您需要创建一个展示者
<?php namespace App\Presenters; use Modules\Crud\Contracts\DatatablePresenterContract; class ArticleModuleDatatablePresenter implements DatatablePresenterContract { /** * Get the datatable columns config/mapping. * * @return array */ public function getDatatableColumns(): array { return [ 'id' => 'ID', 'is_published' => 'Is published', 'title' => [ 'title' => 'Article title', // column title 'data' => 'title', // column data field 'name' => 'translations.title', // SQL column name 'searchable' => true, // Is searchable? 'orderable' => true, // Is orderable? ], 'created_at' => 'Added at', ]; } /** * Get the list relations that should be eager loaded. * * @return array */ public function eagerLoadableRelations(): array { return ['translations']; } /** * Get the columns that should not be escaped. * * @return array */ public function getRawColumns(): array { return ['is_published']; } /** -------------------- Column modifiers -------------------- */ /** * Modify is_published column. * * @param $row * @return string */ public function isPublishedModifier($row) { $labelClass = $row->is_published ? 'success' : 'danger'; $labelText = $row->is_published ? 'Yes' : 'No'; return "<span class=\"label label-{$labelClass}\">{$labelText}</span>"; } }
然后,您需要在CRUD模型中覆盖/设置此展示者
class Article extends Model { use CRUDModel; ... /** * Get the presenter class for datatable. * * @return string */ public function getDatatablePresenter(): string { return \App\Presenters\ArticleModuleDatatablePresenter::class; } }
就这样!现在数据表将使用展示者的列/配置。
可翻译字段
此模块还可以保存可翻译字段。
首先安装module-translate
然后,您需要创建一个带有Translatable和SyncTranslations特质的可翻译模型。之后,只需添加$translationModel和$translatedAttributes,CRUD模块将处理其余部分。
class FoodType extends Model { use Translatable, SyncTranslations, CRUDModel; protected $fillable = [ 'example_1', 'example_2', 'example_3', 'example_4', ]; /** * @var string */ public $translationModel = FoodTypeTranslation::class; /** * @var array */ public $translatedAttributes = [ 'name', ]; /** * @var array */ protected $with = ['translations']; }
文件
如果您想向CRUD模型添加文件,您需要实现StaplerableInterface并添加EloquentTrait
之后,使用$this->hasAttachedFile()
添加文件字段,您就可以开始了
class TestFiles extends Model implements StaplerableInterface { use CRUDModel, EloquentTrait; /** * @var array */ protected $fillable = ['image']; /** * TestFiles constructor. * @param array $attributes */ public function __construct(array $attributes = array()) { $this->hasAttachedFile('image', [ 'url' => '/uploads/portfolio/:id_partition/:filename', ]); parent::__construct($attributes); } }