| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?php
- namespace App\Admin\Controllers;
- use App\Admin\Repositories\Product;
- use App\Admin\Repositories\ProductType;
- use App\Models\User;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use Dcat\Admin\Http\Controllers\AdminController;
- class ProductController extends AdminController
- {
- /**
- * Make a grid builder.
- *
- * @return Grid
- */
- protected function grid()
- {
- return Grid::make(new Product(), function (Grid $grid) {
- $grid->model()->with('user:id,name,email,member_type')->orderByDesc('id');
- $grid->column('id')->sortable();
- $grid->column('user.name',admin_trans_field('user_id'));
- $grid->column('user.email',admin_trans_field('email'));
- $grid->column('user.member_type_text',admin_trans_field('member_type'));
- $grid->column('name');
- $grid->column('content');
- $grid->column('image')->image('',40);
- $grid->column('url')->link();
- $grid->column('type',admin_trans_field('type'))->display(function (){
- $arr = \App\Models\ProductType::query()->whereIn('id',$this->type)->pluck('zh_name');
- return $arr;
- })->label();
- $grid->column('status')->switch();
- $grid->column('created_at');
- $grid->disableCreateButton();
- $grid->filter(function (Grid\Filter $filter) {
- $filter->panel();
- $filter->like('name')->width(3);
- $filter->where('type',function ($query){
- $query->whereJsonContains('type',[intval($this->input)]);
- })->select(\App\Models\ProductType::selectOptions())->width(4);
- });
- });
- }
- /**
- * Make a show builder.
- *
- * @param mixed $id
- *
- * @return Show
- */
- protected function detail($id)
- {
- return Show::make($id, new Product(), function (Show $show) {
- $show->field('id');
- $show->field('type');
- $show->field('user_id');
- $show->field('name');
- $show->field('content');
- $show->field('image');
- $show->field('status');
- $show->field('created_at');
- $show->field('updated_at');
- });
- }
- /**
- * Make a form builder.
- *
- * @return Form
- */
- protected function form()
- {
- return Form::make(new Product(), function (Form $form) {
- $form->display('id')->width(4);
- $form->multipleSelect('type')
- ->options(\App\Models\ProductType::selectOptions())
- ->saving(function ($value) {
- // 转化成json字符串保存到数据库
- return json_encode($value);
- });
- // $form->select('type')->options(\App\Models\ProductType::selectOptions())->required()->width(4);
- $form->select('user_id')->width(4)->options(User::query()->where('status',1)->pluck('name','id'))->required();
- $form->text('name')->required()->width(4);
- $form->textarea('content')->required()->width(4);
- $form->image('image')->autoUpload()->uniqueName()->required()->width(4);
- $form->url('url')->width(4);
- $form->switch('status')->default(1);
- });
- }
- }
|