GroupController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace App\Module\GameItems\AdminControllers;
  3. use App\Module\GameItems\Models\ItemGroup;
  4. use App\Module\GameItems\Models\Item as ItemItem;
  5. use Dcat\Admin\Form;
  6. use Dcat\Admin\Grid;
  7. use Dcat\Admin\Show;
  8. use UCore\DcatAdmin\AdminController;
  9. use Spatie\RouteAttributes\Attributes\Resource;
  10. #[Resource('game-items-groups', names: 'dcat.admin.game-items-groups')]
  11. class GroupController extends AdminController
  12. {
  13. /**
  14. * 标题
  15. *
  16. * @var string
  17. */
  18. protected $title = '物品组管理';
  19. /**
  20. * 列表页
  21. *
  22. * @return Grid
  23. */
  24. protected function grid()
  25. {
  26. return Grid::make(new ItemGroup(), function (Grid $grid) {
  27. $grid->column('id', 'ID')->sortable();
  28. $grid->column('name', '名称');
  29. $grid->column('code', '编码');
  30. $grid->column('description', '描述')->limit(30);
  31. $grid->column('created_at', '创建时间');
  32. $grid->column('updated_at', '更新时间');
  33. // 筛选
  34. $grid->filter(function ($filter) {
  35. $filter->equal('id', 'ID');
  36. $filter->like('name', '名称');
  37. $filter->like('code', '编码');
  38. });
  39. });
  40. }
  41. /**
  42. * 详情页
  43. *
  44. * @param mixed $id
  45. * @return Show
  46. */
  47. protected function detail($id)
  48. {
  49. return Show::make(ItemGroup::findOrFail($id), function (Show $show) {
  50. $show->field('id', 'ID');
  51. $show->field('name', '名称');
  52. $show->field('code', '编码');
  53. $show->field('description', '描述');
  54. $show->field('created_at', '创建时间');
  55. $show->field('updated_at', '更新时间');
  56. // 显示物品组中的物品
  57. $show->groupItems('物品组内容', function ($groupItems) {
  58. $groupItems->resource('/admin/game-items-group-items');
  59. $groupItems->id('ID');
  60. $groupItems->item()->name('物品名称');
  61. $groupItems->weight('权重');
  62. });
  63. });
  64. }
  65. /**
  66. * 表单
  67. *
  68. * @return Form
  69. */
  70. protected function form()
  71. {
  72. return Form::make(new ItemGroup(), function (Form $form) {
  73. $form->text('name', '名称')->required();
  74. $form->text('code', '编码')->required()->help('用于系统识别的唯一编码');
  75. $form->textarea('description', '描述');
  76. // 物品组内容
  77. $form->hasMany('groupItems', '物品组内容', function (Form\NestedForm $form) {
  78. $form->select('item_id', '物品')
  79. ->options(ItemItem::pluck('name', 'id'))
  80. ->required();
  81. $form->number('weight', '权重')
  82. ->default(1.0)
  83. ->min(0.001)
  84. ->step(0.001)
  85. ->required()
  86. ->help('权重越高,随机选择时概率越大');
  87. });
  88. });
  89. }
  90. }