MentalityDrillsSubjectController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Models\MentalityDrill;
  4. use App\Models\MentalityDrillCategory;
  5. use App\Models\MentalityDrillsSubject;
  6. use Encore\Admin\Controllers\AdminController;
  7. use Encore\Admin\Form;
  8. use Encore\Admin\Grid;
  9. use Encore\Admin\Show;
  10. use Illuminate\Support\Facades\DB;
  11. /**
  12. * 课程相关控制
  13. */
  14. class MentalityDrillsSubjectController extends AdminController
  15. {
  16. /**
  17. * Title for current resource.
  18. *
  19. * @var string
  20. */
  21. protected $title = '课程';
  22. /**
  23. * Make a grid builder.
  24. *
  25. * @return Grid
  26. */
  27. protected function grid()
  28. {
  29. $grid = new Grid(new MentalityDrillsSubject());
  30. $grid->filter(function($filter){
  31. $filter->disableIdFilter();
  32. // $filter->where(function($query){
  33. // $query->where('activity_range_id=' . $_GET['activity_range_id']);
  34. // },'hidden');
  35. // 在这里添加字段过滤器
  36. $filter->like('name', '课程名称');
  37. $tableName = MentalityDrillCategory::query()->getModel()->getTable();
  38. $isSubject = MentalityDrillCategory::isSubjectYes;
  39. $select = MentalityDrill::query()
  40. ->whereRaw(DB::raw("category_id in (select id from {$tableName} where is_subject = {$isSubject} and deleted_at is null)"))
  41. ->get(['id','title as name'])
  42. ->pluck('name','id');
  43. $filter->equal('mentality_drills_id', '训练')->select(
  44. $select
  45. );
  46. $filter->expand();
  47. });
  48. $grid->column('id', __('Id'));
  49. $grid->column('mentality_drills_id', __('训练'))->display(function ($id) {
  50. if ($id > 0){
  51. $mentalityDrillData = MentalityDrill::find($id);
  52. if ($mentalityDrillData){
  53. return $mentalityDrillData->title;
  54. }
  55. }
  56. return '';
  57. });
  58. $grid->column('name', __('课程名称'));
  59. $grid->column('time', __('时长'));
  60. // $grid->column('type', __('类别'))->display(function ($type) {
  61. // return MentalityDrillsSubject::$typeMap[$type];
  62. // });
  63. $grid->column('created_at', __('创建时间'));
  64. $grid->column('updated_at', __('更新时间'));
  65. return $grid;
  66. }
  67. /**
  68. * Make a show builder.
  69. *
  70. * @param mixed $id
  71. * @return Show
  72. */
  73. protected function detail($id)
  74. {
  75. $show = new Show(MentalityDrillsSubject::findOrFail($id));
  76. $show->field('id', __('Id'));
  77. $show->field('mentality_drills_id', __('Drill id'));
  78. $show->field('name', __('Name'));
  79. $show->field('time', __('Time'));
  80. $show->field('src', __('Src'));
  81. $show->field('created_at', __('Created at'));
  82. $show->field('updated_at', __('Updated at'));
  83. $show->field('deleted_at', __('Deleted at'));
  84. return $show;
  85. }
  86. /**
  87. * Make a form builder.
  88. *
  89. * @return Form
  90. */
  91. protected function form()
  92. {
  93. $form = new Form(new MentalityDrillsSubject());
  94. $tableName = MentalityDrillCategory::query()->getModel()->getTable();
  95. $isSubject = MentalityDrillCategory::isSubjectYes;
  96. $select = MentalityDrill::query()
  97. ->whereRaw(DB::raw("category_id in (select id from {$tableName} where is_subject = {$isSubject} and deleted_at is null)"))
  98. ->get(['id','title as name'])
  99. ->pluck('name','id');
  100. $form->select('mentality_drills_id', __('训练'))->options($select)->required();
  101. // $form->select('type', __('类别'))->options(MentalityDrillsSubject::$typeMap)->required();
  102. $form->hidden("type","类别")->value(MentalityDrillsSubject::TYPE_2);
  103. $form->text('name', __('课程名称'))->required();
  104. $form->text('time', __('时长'))->required();
  105. $form->file('src', __('视频'))->removable()->uniqueName()->move('videos/'.date('Y').'/'.date('m'));
  106. $form->image('pic', __('背景图片'));
  107. return $form;
  108. }
  109. }