Axios、Laravel 和 VueJS 中的删除方法 [英] Delete Method in Axios, Laravel and VueJS

查看:52
本文介绍了Axios、Laravel 和 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 所以在我的销毁方法中,我可以获取 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})

在我的销毁方法中:

    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 有什么区别?

Any difference between using api/users/destroy vs api/users only?

感谢您对此的任何帮助!

Thanks for any help on this!

推荐答案

这是因为方法签名.使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆