ProductController.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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(['type:id,zh_name,ko_name','user:id,name'])->orderByDesc('id');
  21. $grid->column('id')->sortable();
  22. $grid->column('image')->image('',40);
  23. $grid->column('type.zh_name',admin_trans_field('type'));
  24. $grid->column('user.name',admin_trans_field('user_id'));
  25. $grid->column('name');
  26. $grid->column('content');
  27. $grid->column('status')->switch();
  28. $grid->column('created_at');
  29. $grid->disableCreateButton();
  30. $grid->filter(function (Grid\Filter $filter) {
  31. $filter->panel();
  32. $filter->like('name')->width(3);
  33. $filter->equal('type')->select(\App\Models\ProductType::selectOptions())->width(3);
  34. });
  35. });
  36. }
  37. /**
  38. * Make a show builder.
  39. *
  40. * @param mixed $id
  41. *
  42. * @return Show
  43. */
  44. protected function detail($id)
  45. {
  46. return Show::make($id, new Product(), function (Show $show) {
  47. $show->field('id');
  48. $show->field('type');
  49. $show->field('user_id');
  50. $show->field('name');
  51. $show->field('content');
  52. $show->field('image');
  53. $show->field('status');
  54. $show->field('created_at');
  55. $show->field('updated_at');
  56. });
  57. }
  58. /**
  59. * Make a form builder.
  60. *
  61. * @return Form
  62. */
  63. protected function form()
  64. {
  65. return Form::make(new Product(), function (Form $form) {
  66. $form->display('id')->width(4);
  67. $form->select('type')->options(\App\Models\ProductType::selectOptions())->required()->width(4);
  68. $form->select('user_id')->width(4)->options(User::query()->where('status',1)->pluck('name','id'))->required();
  69. $form->text('name')->required()->width(4);
  70. $form->textarea('content')->required()->width(4);
  71. $form->image('image')->autoUpload()->uniqueName()->required()->width(4);
  72. $form->switch('status')->default(1);
  73. });
  74. }
  75. }