从数据库中删除不适用于Laravel中的API

发布于 2025-02-12 21:39:33 字数 704 浏览 1 评论 0原文

从数据库中删除的过程与Laravel中的API不起作用 删除的函数

  public function deletePatient($id) 
    {  
         $patient =Patient::find($id);
          if(!sizeof($patient)==0) {
            $patient->delete();
          return response()->json([
            "message" => "Patient is  deleted"
          ], 200);
        } else {
          return response()->json([
            "message" => "Patient not found"
          ], 404);
        }
      }

从数据库中

use App\Http\Controllers\Admin\Api\ApisController;
Route::delete('/api/deletepatient/{id}',[ApisController::class,'deletePatient']);

我创建了一个 删除:http:// localhost:8000/api/deletepatient/4

The process of deleting from the database does not work with apis in laravel
I created a function that deletes from database but my code doesn't work

  public function deletePatient($id) 
    {  
         $patient =Patient::find($id);
          if(!sizeof($patient)==0) {
            $patient->delete();
          return response()->json([
            "message" => "Patient is  deleted"
          ], 200);
        } else {
          return response()->json([
            "message" => "Patient not found"
          ], 404);
        }
      }

route

use App\Http\Controllers\Admin\Api\ApisController;
Route::delete('/api/deletepatient/{id}',[ApisController::class,'deletePatient']);

Postman
Delete : http://localhost:8000/api/deletepatient/4

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情绪操控生活 2025-02-19 21:39:33

您似乎有些复杂的事情,请尝试以下操作:

public function deletePatient(Patient $id)
{
    if (!$patient->delete()) {
        return response()->json(['message' => 'Failed to delete patient'], 404);
    }

    return response()->json(['message' => 'Patient deleted'], 204);
}

我们正在使用路由模型绑定让Laravel自动从数据库中找到相关的患者模型,如果找不到一个人404-找不到我们的错误。另外,您可以使用detter :: findorfail($ id);如果愿意。

如果delete操作未成功,请返回错误响应(您可以将其自定义为自己喜欢),否则返回成功响应。通常这是204-没有内容

You appear to be complicating things a bit, try the following:

public function deletePatient(Patient $id)
{
    if (!$patient->delete()) {
        return response()->json(['message' => 'Failed to delete patient'], 404);
    }

    return response()->json(['message' => 'Patient deleted'], 204);
}

We're using route model binding to have Laravel automatically find the relevant Patient model from the database for us, if one is not found it will throw a 404 - Not Found error for us. Alternatively you could use Patient::findOrFail($id); if you wanted to.

If the delete operation was not successful, return an error response (you can customise this to your liking), otherwise return a success response. Usually this is a 204 - No Content.

浪推晚风 2025-02-19 21:39:33

我不知道错误是什么,但是我建议尝试
在您的路线上没有“/API”,

Route::delete('/deletepatient/{id}',[ApisController::class,'deletePatient']);

我会这样这样做:

public function deletePatient($id) 
{  
     if (!$patient = Patient::find($id)) {
      
         return response()->json([
             "message" => "Patient not found"
         ], 404);            
     }
     
     $patient->delete();

     return response()->json([
          "message" => "Patient has been deleted"
     ], 200);        
    
  }

I do not know what is the error, but I would suggest to try
without "/api" on your route

Route::delete('/deletepatient/{id}',[ApisController::class,'deletePatient']);

I would do it like that:

public function deletePatient($id) 
{  
     if (!$patient = Patient::find($id)) {
      
         return response()->json([
             "message" => "Patient not found"
         ], 404);            
     }
     
     $patient->delete();

     return response()->json([
          "message" => "Patient has been deleted"
     ], 200);        
    
  }
数理化全能战士 2025-02-19 21:39:33

请尝试清除您的缓存:

php artisan optimize
php artisan route:clear
php artisan config:cache
php artisan cache:clear

Please try clear your cache:

php artisan optimize
php artisan route:clear
php artisan config:cache
php artisan cache:clear
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文