BusController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\bus;
  4. use Encore\Admin\Auth\Database\Administrator;
  5. use Encore\Admin\Form;
  6. use Encore\Admin\Grid;
  7. use Encore\Admin\Facades\Admin;
  8. use Encore\Admin\Layout\Content;
  9. use App\Http\Controllers\Controller;
  10. use Encore\Admin\Controllers\ModelForm;
  11. /**
  12. * 公交管理
  13. * Class BusController
  14. * @package App\Admin\Controllers
  15. */
  16. class BusController extends Controller
  17. {
  18. use ModelForm;
  19. /**
  20. * Index interface.
  21. *
  22. * @return Content
  23. */
  24. public function index()
  25. {
  26. return Admin::content(function (Content $content) {
  27. $content->header('公交管理');
  28. $content->description('列表');
  29. $content->body($this->grid());
  30. });
  31. }
  32. /**
  33. * Edit interface.
  34. *
  35. * @param $id
  36. * @return Content
  37. */
  38. public function edit($id)
  39. {
  40. return Admin::content(function (Content $content) use ($id) {
  41. $content->header('公交管理');
  42. $content->description('编辑');
  43. $content->body($this->form()->edit($id));
  44. });
  45. }
  46. /**
  47. * Create interface.
  48. *
  49. * @return Content
  50. */
  51. public function create()
  52. {
  53. return Admin::content(function (Content $content) {
  54. $content->header('公交管理');
  55. $content->description('新增');
  56. $content->body($this->form());
  57. });
  58. }
  59. /**
  60. * Make a grid builder.
  61. *
  62. * @return Grid
  63. */
  64. protected function grid()
  65. {
  66. return Admin::grid(bus::class, function (Grid $grid) {
  67. $grid->id('序号')->sortable();
  68. $grid->run_status('运行状态')->switch([
  69. 'on' => ['value' => 1, 'text' => '运行', 'color' => 'success'],
  70. 'off' => ['value' => 0, 'text' => '停运', 'color' => 'default'],
  71. ]);;
  72. $grid->column('number', '编号');
  73. $grid->column('name', '名称');
  74. $grid->column('name', '名称');
  75. $grid->column('运行时间')->display(function () {
  76. return $this->star_time . '-' . $this->end_time;
  77. });;
  78. $grid->column('create_id', '创建者')->display(function ($value){
  79. $name = Administrator::where('id',$value)->pluck('name')->first();
  80. return $name;
  81. });
  82. $grid->column('update_id', '更新者')->display(function ($value){
  83. $name = Administrator::where('id',$value)->pluck('name')->first();
  84. return $name;
  85. });;
  86. $grid->created_at('创建时间');
  87. $grid->updated_at('修改时间');
  88. $grid->disableExport();
  89. });
  90. }
  91. /**
  92. * Make a form builder.
  93. *
  94. * @return Form
  95. */
  96. protected function form()
  97. {
  98. return Admin::form(bus::class, function (Form $form) {
  99. $form->display('id', '序号');
  100. $form->text('number','编号')->rules('required|max:10');
  101. $form->text('name','名称')->rules('required|max:10');
  102. $form->text('address','位置')->rules('required|max:30');
  103. $form->timeRange('star_time', 'end_time', '选择运行时间');
  104. $form->switch('run_status','加入热点')->states([
  105. 'on' => ['value' => 1, 'text' => '运行', 'color' => 'success'],
  106. 'off' => ['value' => 0, 'text' => '停运', 'color' => 'default'],
  107. ])->default(1);
  108. $form->multipleFile('pics','公交照片')->removable()->uniqueName();
  109. $form->display('update_id', '更新者')->with(function ($value){
  110. if($value){
  111. }else{
  112. $value=Admin::user()->id;
  113. }
  114. $name = Administrator::where('id',$value)->pluck('name')->first();
  115. return $name;
  116. });
  117. $form->display('create_id', '创建者')->with(function ($value){
  118. if($value){
  119. }else{
  120. $value=Admin::user()->id;
  121. }
  122. $name = Administrator::where('id',$value)->pluck('name')->first();
  123. return $name;
  124. });
  125. $form->hidden('create_id')->default(Admin::user()->id);
  126. //保存前回调
  127. $form->saving(function (Form $form) {
  128. $form->update_id =Admin::user()->id;
  129. });
  130. $form->display('created_at', '创建时间');
  131. $form->display('updated_at', '更新时间');
  132. });
  133. }
  134. }