ProblemController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\problem;
  4. use App\code;
  5. use Encore\Admin\Auth\Database\Administrator;
  6. use Encore\Admin\Form;
  7. use Encore\Admin\Grid;
  8. use Encore\Admin\Facades\Admin;
  9. use Encore\Admin\Layout\Content;
  10. use App\Http\Controllers\Controller;
  11. use Encore\Admin\Controllers\ModelForm;
  12. /**
  13. * 问题管理
  14. * Class ProblemController
  15. * @package App\Admin\Controllers
  16. */
  17. class ProblemController extends Controller
  18. {
  19. use ModelForm;
  20. /**
  21. * Index interface.
  22. *
  23. * @return Content
  24. */
  25. public function index()
  26. {
  27. return Admin::content(function (Content $content) {
  28. $content->header('问题管理');
  29. $content->description('列表');
  30. $content->body($this->grid());
  31. });
  32. }
  33. /**
  34. * Edit interface.
  35. *
  36. * @param $id
  37. * @return Content
  38. */
  39. public function edit($id)
  40. {
  41. return Admin::content(function (Content $content) use ($id) {
  42. $content->header('问题管理');
  43. $content->description('编辑');
  44. $content->body($this->form()->edit($id));
  45. });
  46. }
  47. /**
  48. * Create interface.
  49. *
  50. * @return Content
  51. */
  52. public function create()
  53. {
  54. return Admin::content(function (Content $content) {
  55. $content->header('问题管理');
  56. $content->description('新增');
  57. $content->body($this->form());
  58. });
  59. }
  60. /**
  61. * Make a grid builder.
  62. *
  63. * @return Grid
  64. */
  65. protected function grid()
  66. {
  67. return Admin::grid(problem::class, function (Grid $grid) {
  68. $grid->id('序号')->sortable();
  69. $grid->column('type', '类别')->display(function ($value){
  70. $name = code::where('id',$value)->pluck('code_dsp_name_cn')->first();
  71. return $name;
  72. });
  73. $grid->column('problem', '问题')->limit(30);
  74. $grid->column('answer', '回答')->limit(30);
  75. $grid->column('create_id', '创建者')->display(function ($value){
  76. $name = Administrator::where('id',$value)->pluck('name')->first();
  77. return $name;
  78. });
  79. $grid->column('update_id', '更新者')->display(function ($value){
  80. $name = Administrator::where('id',$value)->pluck('name')->first();
  81. return $name;
  82. });;
  83. $grid->created_at('创建时间');
  84. $grid->updated_at('修改时间');
  85. $grid->disableExport();
  86. });
  87. }
  88. /**
  89. * Make a form builder.
  90. *
  91. * @return Form
  92. */
  93. protected function form()
  94. {
  95. return Admin::form(problem::class, function (Form $form) {
  96. $form->display('id', '序号');
  97. $form->select('type','类别')->options(function (){
  98. $name = code::where('type_id',15)->pluck('code_dsp_name_cn','id');
  99. return $name;
  100. });
  101. $form->textarea('problem','问题');
  102. $form->textarea('answer','回答');
  103. $form->display('update_id', '更新者')->with(function ($value){
  104. if($value){
  105. }else{
  106. $value=Admin::user()->id;
  107. }
  108. $name = Administrator::where('id',$value)->pluck('name')->first();
  109. return $name;
  110. });
  111. $form->display('create_id', '创建者')->with(function ($value){
  112. if($value){
  113. }else{
  114. $value=Admin::user()->id;
  115. }
  116. $name = Administrator::where('id',$value)->pluck('name')->first();
  117. return $name;
  118. });
  119. $form->hidden('create_id')->default(Admin::user()->id);
  120. //保存前回调
  121. $form->saving(function (Form $form) {
  122. $form->update_id =Admin::user()->id;
  123. });
  124. $form->display('created_at', '创建时间');
  125. $form->display('updated_at', '更新时间');
  126. });
  127. }
  128. }