TextbookController.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Services\TextbookApiService;
  4. use Filament\Notifications\Notification;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Log;
  7. use Illuminate\Support\Facades\Redirect;
  8. class TextbookController extends Controller
  9. {
  10. protected TextbookApiService $apiService;
  11. public function __construct(TextbookApiService $apiService)
  12. {
  13. $this->apiService = $apiService;
  14. }
  15. /**
  16. * 删除教材 - 通过URL参数获取ID,完全绕过Filament的$record传递问题
  17. * 使用GET方法避免CSRF问题
  18. */
  19. public function delete(Request $request, int $id)
  20. {
  21. Log::info('TextbookController::delete called', [
  22. 'textbook_id' => $id,
  23. 'request_all' => $request->all()
  24. ]);
  25. if (!$id) {
  26. Notification::make()
  27. ->title('错误')
  28. ->body('无效的教材ID。')
  29. ->danger()
  30. ->send();
  31. return Redirect::route('filament.admin.resources.textbooks.index');
  32. }
  33. try {
  34. $deleted = $this->apiService->deleteTextbook($id);
  35. Log::info('Delete API result', [
  36. 'deleted' => $deleted,
  37. 'textbook_id' => $id
  38. ]);
  39. if ($deleted) {
  40. Notification::make()
  41. ->title('成功')
  42. ->body('教材删除成功。')
  43. ->success()
  44. ->send();
  45. } else {
  46. Notification::make()
  47. ->title('错误')
  48. ->body('删除失败,请重试。')
  49. ->danger()
  50. ->send();
  51. }
  52. } catch (\Exception $e) {
  53. Log::error('Delete error', [
  54. 'error' => $e->getMessage(),
  55. 'textbook_id' => $id
  56. ]);
  57. Notification::make()
  58. ->title('错误')
  59. ->body('删除失败:' . $e->getMessage())
  60. ->danger()
  61. ->send();
  62. }
  63. // 无论成功失败,都重定向回列表页面
  64. return Redirect::route('filament.admin.resources.textbooks.index');
  65. }
  66. }