英文:
Why laravel eloquent multiple delete with ajax query not working properly?
问题
我正在尝试从数据库中删除多行记录。
除了在 ID 数组中传递的最后一行之外,一切都正常。
我是通过 Ajax 进行操作的,以下是发送的链接
http://local.domain.com/app/deletemultiple/[88,87]
这是我的 API 控制器中的代码
/**
* 从存储中移除指定的资源。
*
* @param $ids
* @return JsonResponse
*/
public function deletemultiple($ids) {
$idarray = json_decode($ids, TRUE);
try {
//$members = Member::whereIn('id', $idarray)->get();
//return response()->json(array('success' => $members));
$ids = explode(",", $ids);
Member::whereIn('id', $ids)->delete();
return response()->json(array('success' => $ids));
} catch(\Exception $e) {
return response()->json(array('error' => $e->getMessage()), 400);
}
}
// 路由如下
Route::delete('/app/deletemultiple/{ids}', [MemberController::class, 'deletemultiple'])->name('member.deletemultiple');
无论选择和传递多少行,除了数组中的最后一行之外,所有行都被删除。如果我将单个 ID 作为数组传递,也不会被删除。
如果取消注释此代码
//$members = Member::whereIn('id', $idarray)->get();
//return response()->json(array('success' => $members));
我会得到传递的所有 ID 的正确响应。
期望能够删除所选的所有行。
英文:
I am trying to delete multiple rows from database.
Everything works fine except last row passed in id array is not deleted.
I am doing it with ajax and below is the link sent
http://local.domain.com/app/deletemultiple/[88,87]
And here is the code in my api controller
/**
* Remove the specified resources from storage.
*
* @param $ids
* @return JsonResponse
*/
public function deletemultiple($ids) {
$idarray = json_decode($ids, TRUE);
try {
//$members = Member::whereIn('id', $idarray)->get();
//return response()->json(array('success' => $members));
$ids = explode(",", $ids);
Member::whereIn('id', $ids)->delete();
return response()->json(array('success' => $ids));
} catch(\Exception $e) {
return response()->json(array('error' => $e->getMessage()), 400);
}
}
//Route is as follows
Route::delete('/app/deletemultiple/{ids}', [MemberController::class, 'deletemultiple'])->name('member.deletemultiple');
No matter how much rows selected and passed. All rows are deleted except the last one in array. If i pass single id as array that is also not deleted.
If I uncomment this code
//$members = Member::whereIn('id', $idarray)->get();
//return response()->json(array('success' => $members));
I get proper response of all ids passed.
Expecting to delete all rows selected.
答案1
得分: 0
在你的控制器中,你正在将 JSON 从 ids 解码为 $idarray,但使用的是 $ids,这会使你的 JSON ($ids) 字符串爆炸成 [1
和 2]
,你应该传递 $idarray 到 whereIn 以供删除,因为此函数期望一个 id 数组(在你的情况下)。
在你的控制器中使用以下代码:
public function deletemultiple($ids) {
$idarray = json_decode($ids, TRUE);
try {
Member::whereIn('id', $idarray)->delete();
return response()->json(array('success' => $idarray));
} catch(\Exception $e) {
return response()->json(array('error' => $e->getMessage()), 400);
}
}
英文:
in your controller you're decoding json from ids to $idarray but using $ids that's exploding your json ($ids) string to [1
and 2]
you should pass in $idarray to whereIn for delete as this function expects array of ids (in your case).
Use this in your controller:
public function deletemultiple($ids) {
$idarray = json_decode($ids, TRUE);
try {
Member::whereIn('id', $idarray)->delete();
return response()->json(array('success' => $idarray));
} catch(\Exception $e) {
return response()->json(array('error' => $e->getMessage()), 400);
}
}
答案2
得分: -1
为什么不使用POST方法,然后传递一个要删除的ID的JSON数组,而不是将多个ID传递到URL路径参数中。
英文:
Why not use post method then passed a json array of ids to be deleted. Instead of passing multiple ids in the url path parameter.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论