123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- namespace App\Admin\Controllers;
- use App\Models\Product;
- use App\Models\ProductCategory;
- use App\Models\ProductHot;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use Dcat\Admin\Http\Controllers\AdminController;
- class ProductHotController extends AdminController
- {
- const TYPE_normal = 'normal'; // 普通用户
- const TYPE_VIP = 'vip'; // VIP/设计师
- protected $types = ['normal' => 1, 'vip' => 2];
- protected $type = 1;
- public function __construct()
- {
- $route = \request()->route();
- $arr = explode("/",$route->uri);
- $this->type = $this->types[$arr[2]]??1;
- }
- /**
- * Make a grid builder.
- *
- * @return Grid
- */
- protected function grid()
- {
- return Grid::make(new ProductHot(), function (Grid $grid) {
- $grid->model()->orderByDesc('sort');
- $grid->model()->where('type', $this->type);
- $grid->column('id')->sortable();
- $grid->column('cover_img')->image('',80);
- $grid->column('product_id')->display(function (){
- $this->products = $this->product_id;
- $names = $this->products->pluck('name')->toArray();
- return implode('、',$names);
- });
- $grid->column('is_opened')->switch();
- $grid->column('sort')->editable();
- $grid->column('created_at');
- // $grid->filter(function (Grid\Filter $filter) {
- // $filter->equal('id');
- //
- // });
- $grid->disableViewButton();
- });
- }
- /**
- * Make a show builder.
- *
- * @param mixed $id
- *
- * @return Show
- */
- protected function detail($id)
- {
- return Show::make($id, new ProductHot(), function (Show $show) {
- $show->field('id');
- $show->field('type');
- $show->field('product_id');
- $show->field('sort');
- $show->field('is_opened');
- $show->field('created_at');
- $show->field('updated_at');
- });
- }
- /**
- * Make a form builder.
- *
- * @return Form
- */
- protected function form()
- {
- return Form::make(new ProductHot(), function (Form $form) {
- $form->display('id');
- $form->hidden('type')->value($this->type);
- $cates = ProductCategory::select(['id','name'])->where('is_opened',1)->get()->toArray();
- $form->multipleSelect(null,'分类')
- ->options(array_column($cates,'name','id'))
- ->when(2, function (Form $form){
- $products = Product::select(['id','name'])->where('is_opened',1)->get()->toArray();
- $form->multipleSelect('product_id')
- ->options(array_column($products,'name','id'))
- ->required();
- });
- $form->image('cover_img','爆款图片(宽高比 1.34:1)')->saveFullUrl()
- ->uniqueName()->autoUpload()
- ->autoSave(false)
- ->removable(false)
- ->width(4)
- ->required();
- $form->radio('is_opened')->options(config('global.bool_status'))->default(1);
- $form->number('sort');
- $form->disableViewButton();
- $form->disableDeleteButton();
- $form->disableListButton();
- $form->disableEditingCheck();
- $form->disableViewCheck();
- $form->disableCreatingCheck();
- });
- }
- }
|