Delete Method in Axios, Laravel and VueJS

前端 未结 4 1446
忘掉有多难
忘掉有多难 2021-01-11 16:49

I am trying to send a delete request via axios to laravel as follow:

axios.delete(\'api/users/\' + this.checkedNames)
.then((response) => {
    console.log         


        
4条回答
  •  感情败类
    2021-01-11 17:04

    Deleting users in array

    Other good option, is to convert javascript array to string, and pass it has the required parameter, instead of passing object. Here the example:

    In Vue.js 2.5.17+

    //Use the javascript method JSON.stringify to convert the array into string: axios.delete('api/users/' + JSON.stringify(this.checkedNames))

    In Laravel 5.3+

    //Resource default route (you don't need to create, it already exists)
    Route::delete('api/users/{id}', 'UserController@destroy'); 
    
    //In laravel delete method, convert the parameter to original array format 
    public function destroy($id)
    {
        User::destroy(json_decode($id); //converting and deleting users in array 'id'
    }
    

    Deleting single user by id

    Just pass the id. You don't need to convert it.

    In Vue.js 2.5.17+

    axios.delete('api/users/' + id)
    

    In Laravel 5.3+

    You can name the parameter as you wish: user, id, item ,...

    In Laravel 5.6+ < is named as $id //this can be the id or the user object
    In Laravel 5.7+ > is named as $user //this can be the id or the user object
    
    public function destroy($id)
    {
        User::destroy($id); 
    }
    

提交回复
热议问题