You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel API Delete路由无法正常工作的解决方案咨询

Solution to Your Laravel API Delete Issue

First off, the core problem here is that your front-end PHP code is sending a GET request instead of the required DELETE request that your Laravel route is expecting. file_get_contents() defaults to GET, so it’s not hitting your Route::delete() endpoint at all. Let’s fix that step by step.

Step 1: Fix the Front-End PHP to Send a DELETE Request

Replace your current front-end code with this, which properly sends a DELETE request using file_get_contents with custom context options:

$id = 1; // Replace with your actual article ID
$url = 'http://192.168.0.21/api/articles/' . $id;

// Set up context to send DELETE request
$context = stream_context_create([
    'http' => [
        'method' => 'DELETE',
        'header' => 'Content-Type: application/json',
        // Uncomment and add your token if your API requires authentication
        // 'header' => "Content-Type: application/json\r\nAuthorization: Bearer YOUR_API_TOKEN",
    ]
]);

// Execute the DELETE request
$result = file_get_contents($url, false, $context);

// Handle the result
if ($result === false) {
    echo "Failed to delete article";
    // Add error logging here if needed
} else {
    $response = json_decode($result, true);
    echo $response['message'] ?? "Article deleted successfully";
}

Your original code was just fetching the article data (via GET) and modifying the response, which doesn’t actually delete anything from the database—this fixes that by sending the correct request type.

Step 2: Verify Laravel Controller & Route Setup

Your Laravel code is mostly correct, but let’s refine it for clarity and debugging:

  • Ensure your Article model maps correctly to your database table (check the $table property if your table name isn’t the plural of the model name).
  • Route model binding works automatically here since the route parameter (article) matches the controller’s type-hinted variable (Article $article). If you’re using a non-standard primary key (not id), specify it in the model or route.
  • Update your controller to return a more helpful response:
    public function delete(Article $article) {
        $article->delete();
        return response()->json(['message' => 'Article deleted successfully'], 200);
    }
    

Step 3: Check for Authentication/Authorization

If your API uses authentication (like Laravel Passport or Sanctum), make sure your front-end includes the necessary auth headers (like the Bearer token commented in the code above). Without this, Laravel might block the request with a 401 Unauthorized error.

Step 4: Debugging Tips

  • Test the DELETE endpoint directly with tools like Postman first—send a DELETE request to http://192.168.0.21/api/articles/[ID] to confirm it works outside your PHP front-end.
  • Check Laravel’s logs (storage/logs/laravel.log) for any error messages if the request still fails.
  • Verify the article with the given ID actually exists in your database.

内容的提问来源于stack exchange,提问作者The Rock

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:43:09