CopyAction.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace App\Module\Farm\AdminControllers\Actions;
  3. use App\Module\Farm\Models\FarmSeedOutput;
  4. use Dcat\Admin\Grid\RowAction;
  5. use Illuminate\Http\Request;
  6. class CopyAction extends RowAction
  7. {
  8. protected $title = '复制';
  9. public function title()
  10. {
  11. return $this->title;
  12. }
  13. public function handle(Request $request)
  14. {
  15. try {
  16. $id = $this->getKey();
  17. // 查找原始记录
  18. $original = FarmSeedOutput::findOrFail($id);
  19. // 创建新记录
  20. $new = $original->replicate();
  21. $new->save();
  22. return $this->response()
  23. ->success("复制成功 [ID: {$new->id}]")
  24. ->refresh();
  25. } catch (\Exception $e) {
  26. return $this->response()
  27. ->error('复制失败: '.$e->getMessage());
  28. }
  29. }
  30. public function confirm()
  31. {
  32. return ['确定要复制此种子产出配置吗?', '复制操作将创建一个新的种子产出配置记录'];
  33. }
  34. }