EpisodesCategoryController.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Models\EpisodesCategory;
  4. use Dcat\Admin\Form;
  5. use Dcat\Admin\Grid;
  6. use Dcat\Admin\Show;
  7. use Dcat\Admin\Http\Controllers\AdminController;
  8. use Dingo\Api\Http\Request;
  9. class EpisodesCategoryController extends AdminController
  10. {
  11. /**
  12. * Make a grid builder.
  13. *
  14. * @return Grid
  15. */
  16. protected function grid()
  17. {
  18. return Grid::make(new EpisodesCategory(), function (Grid $grid) {
  19. $grid->column('id')->sortable();
  20. $grid->column('name');
  21. $grid->column('pid','父分类')->display(function () {
  22. /* @var EpisodesCategory $this*/
  23. return $this->parent ?$this->parent->name : '';
  24. });;
  25. $grid->column('created_at');
  26. });
  27. }
  28. /**
  29. * Make a show builder.
  30. *
  31. * @param mixed $id
  32. *
  33. * @return Show
  34. */
  35. protected function detail($id)
  36. {
  37. return Show::make($id, new EpisodesCategory(), function (Show $show) {
  38. $show->field('id');
  39. $show->field('name');
  40. $show->field('pid');
  41. $show->field('path');
  42. $show->field('sort');
  43. $show->field('created_at');
  44. $show->field('updated_at');
  45. });
  46. }
  47. /**
  48. * Make a form builder.
  49. *
  50. * @return Form
  51. */
  52. protected function form()
  53. {
  54. return Form::make(new EpisodesCategory(), function (Form $form) {
  55. $categoryModel = app(\App\Models\EpisodesCategory::class);
  56. $options = $categoryModel::select(['id','name'])->where('pid',0)->get()->toArray();
  57. array_unshift($options,['id' => 0, 'name' => '顶级分类']);
  58. $options = array_column($options,'name','id');
  59. $form->display('id');
  60. $form->select('pid')->options($options);
  61. $form->text('name');
  62. });
  63. }
  64. public function options(Request $request)
  65. {
  66. $q = $request->get('q');
  67. $categoryModel = app(\App\Models\EpisodesCategory::class);
  68. return $categoryModel->where('name', 'like', "%$q%")->paginate(null, ['id', 'name as text']);
  69. }
  70. }