模型树动作

运行命令

  1. php artisan admin:action

然后输入 6

  1. Which type of action would you like to make?:
  2. [0] default
  3. [1] grid-batch
  4. [2] grid-row
  5. [3] grid-tool
  6. [4] form-tool
  7. [5] show-tool
  8. [6] tree-tool
  9. > 6 # 输入 6

接着输入 Action 类名称,这里需要输入 大驼峰 风格的英文字母

  1. Please enter a name of action class:
  2. > Copy

类名输入完成之后会出现以下信息让开发者输入类的命名空间,默认的命名空间是 App\Admin\Actions\Tree,这里使用默认的就行

  1. Please enter the namespace of action class [App\Admin\Actions\Tree]:
  2. >

最后生成文件如下

  1. <?php
  2. namespace App\Admin\Actions\Tree;
  3. use Dcat\Admin\Actions\Response;
  4. use Dcat\Admin\Tree\AbstractTool;
  5. use Dcat\Admin\Traits\HasPermissions;
  6. use Illuminate\Contracts\Auth\Authenticatable;
  7. use Illuminate\Database\Eloquent\Model;
  8. use Illuminate\Http\Request;
  9. class Copy extends AbstractTool
  10. {
  11. /**
  12. * 按钮标题
  13. *
  14. * @return string
  15. */
  16. protected $title = 'Title';
  17. /**
  18. * 处理请求,如果不需要接口处理,请直接删除这个方法
  19. *
  20. * @param Request $request
  21. *
  22. * @return Response
  23. */
  24. public function handle(Request $request)
  25. {
  26. return $this->response()
  27. ->success('Processed successfully.')
  28. ->redirect('/');
  29. }
  30. /**
  31. * 如果只是a标签跳转,则在这里返回跳转链接即可
  32. *
  33. * @return string|void
  34. */
  35. protected function href()
  36. {
  37. // return admin_url('auth/users');
  38. }
  39. // 如果你想自定义动作按钮的HTML,可以重写此方法
  40. public function html()
  41. {
  42. return parent::html();
  43. }
  44. /**
  45. * 确认弹窗信息,如不需要可以删除此方法
  46. *
  47. * @return string|array|void
  48. */
  49. public function confirm()
  50. {
  51. // return ['Confirm?', 'contents'];
  52. }
  53. /**
  54. * 权限判断,如不需要可以删除此方法
  55. *
  56. * @param Model|Authenticatable|HasPermissions|null $user
  57. *
  58. * @return bool
  59. */
  60. protected function authorize($user): bool
  61. {
  62. return true;
  63. }
  64. /**
  65. * 返回请求接口的参数,如不需要可以删除此方法
  66. *
  67. * @return array
  68. */
  69. protected function parameters()
  70. {
  71. return [];
  72. }
  73. }

使用

  1. $tree->tools(function (Tree\Tools $tools) {
  2. $tools->add(new Copy());
  3. });