GlobalUserController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Models\GlobalUser;
  4. use App\Models\Tenant;
  5. use Encore\Admin\Controllers\AdminController;
  6. use Encore\Admin\Form;
  7. use Encore\Admin\Grid;
  8. use Encore\Admin\Show;
  9. use Encore\Admin\Widgets\Table;
  10. class GlobalUserController extends AdminController
  11. {
  12. protected $title = '全局用户';
  13. protected $description = '';
  14. /**
  15. * Make a grid builder.
  16. *
  17. * @return Grid
  18. */
  19. protected function grid()
  20. {
  21. $grid = new Grid(new GlobalUser(['tenant']));
  22. $grid->model()->orderByDesc('created_at');
  23. $grid->column('id');
  24. $grid->column('username')->copyable();
  25. $grid->column('tenant.name')->expand(function ($model) {
  26. $tenants = $model->tenant()->get()->map(function ($tenant) {
  27. return $tenant->only(['id', 'name', 'tenancy_db_connection', 'expired_at']);
  28. });
  29. return new Table(['ID', 'Name', 'Db_connection', 'expired_at'], $tenants->toArray());
  30. });
  31. $grid->column('created_at')->datetime();
  32. $grid->column('updated_at')->datetime();
  33. $grid->actions(function (Grid\Displayers\Actions $actions) {
  34. $actions->disableDelete();
  35. });
  36. $grid->filter(function (Grid\Filter $filter) {
  37. $filter->like('username');
  38. $filter->equal('tenant_id')->select(Tenant::query()->pluck('name', 'id'));
  39. });
  40. return $grid;
  41. }
  42. /**
  43. * Make a show builder.
  44. *
  45. * @param mixed $id
  46. *
  47. * @return Show
  48. */
  49. protected function detail($id)
  50. {
  51. $show = new Show(GlobalUser::query()->findOrFail($id));
  52. $show->field('id');
  53. $show->field('username');
  54. $show->field('created_at');
  55. $show->field('updated_at');
  56. $show->tenant('tenant', function ($tenant) {
  57. $tenant->id();
  58. $tenant->name();
  59. $tenant->tenancy_db_connection();
  60. $tenant->expired_at();
  61. $tenant->panel()->tools(function ($tools) {
  62. $tools->disableList();
  63. $tools->disableEdit();
  64. $tools->disableDelete();
  65. });
  66. });
  67. return $show;
  68. }
  69. /**
  70. * Make a form builder.
  71. *
  72. * @return Form
  73. */
  74. protected function form()
  75. {
  76. $form = new Form(new GlobalUser());
  77. $form->text('username')->placeholder('unique email or mobile')->rules(function ($form) {
  78. return 'required|unique:global_users,username,' . $form->model()->id;
  79. })->required();
  80. $form->select('tenant_id', 'Tenant')
  81. ->options(function () {
  82. return Tenant::all()->pluck('name', 'id')->toArray();
  83. })
  84. ->required();
  85. return $form;
  86. }
  87. }