Laravel API DELETE 资源控制器请求并收到 404 Not Found 错误(第二次删除)

问题描述 投票:0回答:1

问题正文:
我在 Laravel API 设置中遇到了一个特殊问题。我有一个资源控制器用于管理学生数据,包括删除。当我向

http://127.0.0.1:8000/api/student/1
发出 DELETE 请求(例如,使用
id=1
)时,第一个请求成功删除学生并返回带有成功消息的 JSON 响应:

{
    "message": "Student deleted successfully"
}

但是,如果我随后使用相同的 URL 和 ID 立即发出另一个 DELETE 请求,我不会收到类似的成功消息,而是会收到 404 Not Found 错误以及呈现的 HTML 响应。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Not Found</title>

    <style>
        /* code css here */
    </style>
    <link rel='stylesheet' type='text/css' property='stylesheet'
        href='//127.0.0.1:8000/_debugbar/assets/stylesheets?v=1697098252&theme=auto' data-turbolinks-eval='false'
        data-turbo-eval='false'>
    <script src='//127.0.0.1:8000/_debugbar/assets/javascript?v=1697098252' data-turbolinks-eval='false'
        data-turbo-eval='false'></script>
    <script data-turbo-eval="false">
        jQuery.noConflict(true);
    </script>
    <script>
        //js code here 
    </script>
</head>

<body class="antialiased">
    <div
        class="relative flex items-top justify-center min-h-screen bg-gray-100 dark:bg-gray-900 sm:items-center sm:pt-0">
        <div class="max-w-xl mx-auto sm:px-6 lg:px-8">
            <div class="flex items-center pt-8 sm:justify-start sm:pt-0">
                <div class="px-4 text-lg text-gray-500 border-r border-gray-400 tracking-wider">
                    404 </div>

                <div class="ml-4 text-lg text-gray-500 uppercase tracking-wider">
                    Not Found </div>
            </div>
        </div>
    </div>
    <script type="text/javascript">
        //js script code
    </script>
</body>

</html>

Controller函数的代码

// StudentController.php

<?php

use App\Models\Student;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;

class StudentController extends Controller
{
    // ...other code 

    public function destroy(Student $student)
    {
        try {
            $student->delete();
            $student->person()->delete();
            $student->guardian1()->delete();
            $student->guardian2()->delete();
            return response()->json(['message' => 'Student deleted successfully'], 200);
        } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $notFoundException) {
            return response()->json(['error' => 'Student not found', 'exception' => $notFoundException->getMessage()], 404);
        } catch (\Exception $e) {
            return response()->json(['error' => 'An error occurred while deleting the student', 'exception' => $e->getMessage()], 500);
        }
    }
}

Api路由代码

// api.php

<?php

use Illuminate\Support\Facades\Route;

Route::group(['middleware' => 'auth:api'], function(){
    // ...other code 

    Route::resource('student', App\Http\Controllers\StudentController::class);
 
});

附加信息:

  • 每当我第二次尝试删除学生时,都会出现这种行为。
  • 我已确保在
    api.php
    中注册了适当的路线。
  • 我正在使用 Laravel 版本 10.32.1

我预计两个 DELETE 请求都会成功返回 JSON 响应,其中包含指示成功删除的消息,或不存在元素的错误消息。

laravel http-status-code-404 laravel-routing laravel-10 laravel-resource
1个回答
0
投票

确保为您要检查的异常类指定别名。

use Illuminate\Database\Eloquent\ModelNotFoundException;

如果没有这个,您将检查

App\Exceptions\ModelNotFoundException.

的实例
© www.soinside.com 2019 - 2024. All rights reserved.