Axios,Laravel和VueJS中的删除方法 [英] Delete Method in Axios, Laravel and VueJS
问题描述
我正尝试通过axios向laravel发送删除请求:
I am trying to send a delete request via axios to laravel as follow:
axios.delete('api/users/' + this.checkedNames)
.then((response) => {
console.log(response)
}, (error) => {
// error callback
})
现在从axios文档中我读到,对于删除请求,我们应该使用configObject,这样上面的内容可以这样重写:
Now from axios documentation I read that for delete request we should be using a configObject so the above could be rewritten as so:
axios.delete('api/users/', {params: {ids:
this.checkedNames})
.then((response) => {
console.log(response)
}, (error) => {
// error callback
})
然后api.php中有Route::resource('users', 'UsersController');
,因此删除的默认路由为:
I have then Route::resource('users', 'UsersController');
in api.php so the default route for deleting is:
DELETE| api/users/{user}| users.destroy
,控制器的方法是:
|App\Http\Controllers\UsersController@destroy
当我传递一个id(例如api/users/12)时,我能够按预期删除用户,它可以正确删除,但是当我尝试传递数组时,事情变得复杂了.
I am able to delete as expected a user when I pass a single id let's say api/users/12, it gets deleted correctly but when I try to pass the array above things get complicated.
如果我按照axios文档axios.delete('api/users/', {params: {id: this.checkedNames}})
进行尝试,则看来我正在发送此http://lorillacrud.dev/api/users?id[]=21&id[]=20
,但是我得到了405方法不允许.
if I try as per axios documentation axios.delete('api/users/', {params: {id: this.checkedNames}})
it looks I am sending this http://lorillacrud.dev/api/users?id[]=21&id[]=20
but I get a 405 method not allowed.
如果我尝试axios.delete('api/users/' + this.checkedNames )
我会得到http://lorillacrud.dev/api/users/20,21
,那么在我的destroy方法中,我可以获取ID并删除,但是我想知道这是否是正确的方法?
if I try axios.delete('api/users/' + this.checkedNames )
I get http://lorillacrud.dev/api/users/20,21
so in my destroy method I could grab the ids and delete, but I am wondering if this is the correct way to do it?
我似乎使它工作了,但是我不明白,所以仍然有任何帮助让我了解我实际上在做些什么!
I seemed I made it work but I am not understanding so any help still appreciated to make a sense of what I am actually making work!
因此,如果更改为:
axios.delete('api/users/destroy', {params: {'id': this.checkedNames})
以及我的destroy方法:
and in my destroy method:
if ($request->id) {
foreach ($request->id as $id) {
User::destroy($id);
}
}
User::destroy($id);
}
所以...
// not deletes the user
axios.delete('api/users/destroy', {params: {id: id}})
// ok deletes the users when using request->id in a for loop in the destroy laravel method.
axios.delete('api/users/destroy', {params: {ids: this.checkedNames}})
// ok deletes one user
axios.delete('api/users/' + id)
对不起,但是我很困惑为什么和什么!
sorry guys but I have a lot of confusion why and what !!!
路由名称为user.destroy
,为什么当我传递数组时它起作用,而当我传递单个值时它不起作用,为什么反之亦然,带有方法delete的路由在传递数组时不会删除???
The route name is user.destroy
why does it work when I pass an array and it does not when I pass a single value, why viceversa the route with method delete will not delete when pass an array ???
仅使用api/users/destroy
与api/users
有什么区别?
谢谢您的帮助!
推荐答案
这是由于方法签名所致.使用Resource
时,默认的delete
路由需要一个参数.所以当做:
It is because of the method signatures. The default delete
route when using Resource
expects a single parameter. So when doing:
axios.delete('api/users', {params: {'id': this.checkedNames})
您缺少必需参数.路线定义为
you are missing a required parameter. The route definition is
Route::delete('api/users/{id}', 'UserController@destroy');
// You are missing `id` here. So it won't work.
通常,如果您打算偏离默认行为,建议创建自己的函数.因此,您可以保留默认的destroy($id)
函数,以删除单个条目并编写一个将删除许多条目的新函数.首先为其添加一条路线
Usually, if you are going to stray away from the default behavior, it is recommended to create your own function. So you could leave the default destroy($id)
function as is to delete a single entry and write a new function that will delete many. Start by adding a route for it
Route::delete('api/users', 'UserController@deleteMany');
然后定义要处理的函数
public function deleteMany(Request $request)
{
try
{
User::whereIn('id', $request->id)->delete(); // $request->id MUST be an array
return response()->json('users deleted');
}
catch (Exception $e) {
return response()->json($e->getMessage(), 500);
}
}
总而言之,您的问题来自路由定义.您来自Axios的路线与来自Laravel的路线定义(405)不匹配.
To summarise, your problem came from route definition. Your route from Axios did not match the route definition from Laravel, hence the 405.
这篇关于Axios,Laravel和VueJS中的删除方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!