ProductController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Admin\Repositories\Product;
  4. use App\Admin\Repositories\ProductType;
  5. use App\Models\User;
  6. use Dcat\Admin\Form;
  7. use Dcat\Admin\Grid;
  8. use Dcat\Admin\Show;
  9. use Dcat\Admin\Http\Controllers\AdminController;
  10. class ProductController extends AdminController
  11. {
  12. /**
  13. * Make a grid builder.
  14. *
  15. * @return Grid
  16. */
  17. protected function grid()
  18. {
  19. return Grid::make(new Product(), function (Grid $grid) {
  20. $grid->model()->with('user:id,name,email,member_type')->orderByDesc('id');
  21. $grid->column('id')->sortable();
  22. $grid->column('user.name',admin_trans_field('user_id'));
  23. $grid->column('user.email',admin_trans_field('email'));
  24. $grid->column('user.member_type_text',admin_trans_field('member_type'));
  25. $grid->column('name');
  26. $grid->column('content');
  27. $grid->column('image')->image('',40);
  28. $grid->column('url')->link();
  29. $grid->column('type',admin_trans_field('type'))->display(function (){
  30. $arr = \App\Models\ProductType::query()->whereIn('id',$this->type)->pluck('zh_name');
  31. return $arr;
  32. })->label();
  33. $grid->column('status')->switch();
  34. $grid->column('created_at');
  35. $grid->disableCreateButton();
  36. $grid->filter(function (Grid\Filter $filter) {
  37. $filter->panel();
  38. $filter->like('name')->width(3);
  39. $filter->where('type',function ($query){
  40. $query->whereJsonContains('type',[intval($this->input)]);
  41. })->select(\App\Models\ProductType::selectOptions())->width(4);
  42. });
  43. });
  44. }
  45. /**
  46. * Make a show builder.
  47. *
  48. * @param mixed $id
  49. *
  50. * @return Show
  51. */
  52. protected function detail($id)
  53. {
  54. return Show::make($id, new Product(), function (Show $show) {
  55. $show->field('id');
  56. $show->field('type');
  57. $show->field('user_id');
  58. $show->field('name');
  59. $show->field('content');
  60. $show->field('image');
  61. $show->field('status');
  62. $show->field('created_at');
  63. $show->field('updated_at');
  64. });
  65. }
  66. /**
  67. * Make a form builder.
  68. *
  69. * @return Form
  70. */
  71. protected function form()
  72. {
  73. return Form::make(new Product(), function (Form $form) {
  74. $form->display('id')->width(4);
  75. $form->multipleSelect('type')
  76. ->options(\App\Models\ProductType::selectOptions())
  77. ->saving(function ($value) {
  78. // 转化成json字符串保存到数据库
  79. return json_encode($value);
  80. });
  81. // $form->select('type')->options(\App\Models\ProductType::selectOptions())->required()->width(4);
  82. $form->select('user_id')->width(4)->options(User::query()->where('status',1)->pluck('name','id'))->required();
  83. $form->text('name')->required()->width(4);
  84. $form->textarea('content')->required()->width(4);
  85. $form->image('image')->autoUpload()->uniqueName()->required()->width(4);
  86. $form->url('url')->width(4);
  87. $form->switch('status')->default(1);
  88. });
  89. }
  90. }