数据表单动作

运行命令

  1. php artisan admin:action

然后输入 4

  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. > 4 # 输入 4

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

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

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

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

最后生成文件如下

  1. <?php
  2. namespace App\Admin\Actions\Form;
  3. use Dcat\Admin\Actions\Response;
  4. use Dcat\Admin\Form\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. // 获取主键
  27. $key = $this->getKey();
  28. return $this->response()
  29. ->success('Processed successfully.')
  30. ->redirect('/');
  31. }
  32. /**
  33. * 如果只是a标签跳转,则在这里返回跳转链接即可
  34. *
  35. * @return string|void
  36. */
  37. protected function href()
  38. {
  39. // 获取主键
  40. $key = $this->getKey();
  41. // 获取当前页其他字段
  42. $username = $this->parent->model()->username;
  43. // return admin_url('auth/users');
  44. }
  45. // 如果你想自定义动作按钮的HTML,可以重写此方法
  46. public function html()
  47. {
  48. return parent::html();
  49. }
  50. /**
  51. * 确认弹窗信息,如不需要可以删除此方法
  52. *
  53. * @return string|array|void
  54. */
  55. public function confirm()
  56. {
  57. // return ['Confirm?', 'contents'];
  58. }
  59. /**
  60. * 权限判断,如不需要可以删除此方法
  61. *
  62. * @param Model|Authenticatable|HasPermissions|null $user
  63. *
  64. * @return bool
  65. */
  66. protected function authorize($user): bool
  67. {
  68. return true;
  69. }
  70. /**
  71. * 返回请求接口的参数,如不需要可以删除此方法
  72. *
  73. * @return array
  74. */
  75. protected function parameters()
  76. {
  77. return [];
  78. }
  79. }

使用

  1. $form->tools(function (Form\Tools $tools) {
  2. $tools->append(new Copy());
  3. });