lcly-data-admin/app/Admin/Controllers/CropController.php

118 lines
3.7 KiB
PHP

<?php
namespace App\Admin\Controllers;
use App\Http\Resources\CropResource;
use App\Models\Crop;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;
use Peidikeji\Keywords\Models\Keywords;
class CropController extends AdminController
{
/**
* Make a grid builder.
*
* @return Grid
*/
protected function grid()
{
return Grid::make(Crop::with(['category', 'parent']), function (Grid $grid) {
$grid->id();
$grid->column('category.name', '行业');
$grid->column('parent.name', '上级');
// $grid->column('crop_type');
$grid->column('name');
$grid->column('is_end')->bool();
$grid->column('unit');
$grid->column('sort');
// $grid->column('extends');
$grid->column('created_at')->sortable();
// $grid->column('updated_at')
$grid->model()->orderBy('created_at', 'desc');
$grid->showCreateButton(! config('admin.permission.enable') || Admin::user()->can('dcat.admin.crops.create'));
$grid->showQuickEditButton(! config('admin.permission.enable') || Admin::user()->can('dcat.admin.crops.edit'));
$grid->showDeleteButton(! config('admin.permission.enable') || Admin::user()->can('dcat.admin.crops.destroy'));
$grid->filter(function (Grid\Filter $filter) {
$filter->like('name')->width(3);
$filter->equal('category_id')->select(Keywords::where('type_key', 'crops-category')->get()->pluck('name', 'id'))->width(3);
$filter->equal('crop_type')->select([
1=> '基地',
2=> '镇街'
])->width(3);
});
});
}
/**
* Make a show builder.
*
* @param mixed $id
* @return Show
*/
protected function detail($id)
{
return Show::make($id, new Crop(), function (Show $show) {
$show->field('id');
$show->field('category_id');
$show->field('parent_id');
$show->field('crop_type');
$show->field('name');
$show->field('is_end');
$show->field('unit');
$show->field('sort');
$show->field('extends');
$show->field('created_at');
$show->field('updated_at');
});
}
/**
* Make a form builder.
*
* @return Form
*/
protected function form()
{
return Form::make(new Crop(), function (Form $form) {
$form->display('id');
$form->select('category_id')->options(Keywords::where('type_key', 'crops-category')->get()->pluck('name', 'id'));
$form->select('parent_id')->options(Crop::selectOptions(function ($q) {
return $q->where('is_end', 0);
}))->required();
// $form->text('parent_id');
$form->radio('crop_type')->options([
1 => '基地',
2 => '镇街',
])->default(2);
$form->text('name');
$form->switch('is_end');
$form->text('unit');
$form->number('sort');
$form->table('extends', function ($table) {
$table->text('name', '类目');
$table->text('unit', '单位');
})->saving(function ($v) {
return json_encode($v);
});
$form->display('created_at');
$form->display('updated_at');
});
}
public function info($id)
{
$crop = Crop::findOrFail($id);
return CropResource::make($crop);
}
}