从数据库中删除不适用于Laravel中的API
从数据库中删除的过程与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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您似乎有些复杂的事情,请尝试以下操作:
我们正在使用路由模型绑定让Laravel自动从数据库中找到相关的
患者
模型,如果找不到一个人404-找不到我们的错误。另外,您可以使用
detter :: findorfail($ id);
如果愿意。如果
delete
操作未成功,请返回错误响应(您可以将其自定义为自己喜欢),否则返回成功响应。通常这是204-没有内容
。You appear to be complicating things a bit, try the following:
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 a404 - Not Found
error for us. Alternatively you could usePatient::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 a204 - No Content
.我不知道错误是什么,但是我建议尝试
在您的路线上没有“/API”,
我会这样这样做:
I do not know what is the error, but I would suggest to try
without "/api" on your route
I would do it like that:
请尝试清除您的缓存:
Please try clear your cache: