ProductSpecController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Models\Product;
  4. use App\Models\ProductSpec;
  5. use Dcat\Admin\Form;
  6. use Dcat\Admin\Grid;
  7. use Dcat\Admin\Layout\Content;
  8. use Dcat\Admin\Http\Controllers\AdminController;
  9. class ProductSpecController extends AdminController
  10. {
  11. protected $product_id;
  12. protected $product;
  13. public function __construct()
  14. {
  15. $route = \request()->route();
  16. $this->product_id = $route->parameters['id'];
  17. $this->product = Product::find($this->product_id);
  18. }
  19. protected function content(Content $content)
  20. {
  21. return $content
  22. ->translation($this->translation())
  23. ->breadcrumb(
  24. ['text' => $this->product->name,'url' => '#'],
  25. ['text' => '规格管理'],
  26. )
  27. ->title($this->title())
  28. ->description('规格管理');
  29. }
  30. public function index(Content $content, $productId = 0)
  31. {
  32. return $this->content($content)
  33. ->body($this->grid());
  34. }
  35. public function create(Content $content,$productId = 0)
  36. {
  37. return $this->content($content)->body($this->form());
  38. }
  39. public function update($productId, $id = 0)
  40. {
  41. return $this->form()->update($id);
  42. }
  43. public function store($episodeId = 0)
  44. {
  45. return $this->form()->store();
  46. }
  47. public function edit($productId, Content $content, $id = 0)
  48. {
  49. return $this->content($content)->body($this->form()->edit($id));
  50. }
  51. /**
  52. * Make a grid builder.
  53. *
  54. * @return Grid
  55. */
  56. protected function grid()
  57. {
  58. return Grid::make(ProductSpec::where('product_id', $this->product_id), function (Grid $grid) {
  59. $grid->model()->orderByDesc('sort');
  60. $grid->column('id')->sortable();
  61. $grid->column('name')->label('primary');
  62. $grid->column('is_opened')->switch();
  63. $grid->column('sort')->editable();
  64. $grid->column('specs')->display(function (){
  65. $html = '';
  66. foreach ($this->specs as $key => $specs){
  67. $br = $key % 3 == 0 && $key != 0 ? '<br>': '';
  68. $html .= "<span class='label' style='background:#586cb1;margin: 0 5px 5px 0;display: inline-block;'> {$specs['name']}(&yen;{$specs['price']})</span>{$br}";
  69. }
  70. return $html;
  71. });
  72. $grid->disableViewButton();
  73. });
  74. }
  75. /**
  76. * Make a form builder.
  77. *
  78. * @return Form
  79. */
  80. protected function form()
  81. {
  82. return Form::make(new ProductSpec(), function (Form $form) {
  83. $form->display('id');
  84. $form->hidden('product_id')->value($this->product_id);
  85. $form->text('name')->required();
  86. $form->radio('is_opened')->options(config('global.bool_status'))->default(1);
  87. $form->number('sort');
  88. $form->table('specs', '规格', function (Form\NestedForm $table){
  89. $table->text('name','规格名称')->required();
  90. $table->decimal('price','规格价格')->required();
  91. });
  92. $form->disableViewButton();
  93. $form->disableDeleteButton();
  94. $form->disableListButton();
  95. $form->disableEditingCheck();
  96. $form->disableViewCheck();
  97. $form->disableCreatingCheck();
  98. });
  99. }
  100. }