Gohar ul Islam
Gohar ul Islam

Reputation: 61

Axios Delete not working

In my CRUD webapp based on Laravel 5.6 and Vue.js 2 Add, Edit and Show are working fine. But Delete is not working because axios.delete is not sending id to controller. Console.log in Home.vue is showing correct id and key values. Following is the code and result what I am getting in controller. Please tell my mistake. Also do I really need a Route for delete in web.php?

Controller

public function destroy(Sms $sms)
    {
        $myfile = fopen("newfile.txt", "w", true) or die("Unable to open file!");
        $txt = "Print_r: ". print_r($sms) ."\r\nID: ". $sms->id;
        //fwrite($myfile, $txt);
        fwrite($myfile, print_r($sms, TRUE));
        //die();
        Sms::where('id',$sms->id)->delete();

}

Home.vue

del(key,id){
            if(confirm("Are you sure?")){
                this.loading = !this.loading
                /*axios.delete(`/sms/${id}`)*/
                axios.delete(`sms/${id}`, {params: {id: `${id}`}})
                .then((response)=> {this.lists.splice(key,1);this.loading = !this.loading})
                .catch((error)=> this.errors = error.response.data.errors)
                console.log(`KEY:${key} ID:${id}`);
            }

}

web.php

Route::delete('sms/{id}', 'smsController@destroy');

newfile.txt

App\Sms Object ( [hidden:protected] => Array ( [0] => created_at [1] => updated_at )

[connection:protected] => 
[table:protected] => 
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
    (
    )

[withCount:protected] => Array
    (
    )

[perPage:protected] => 15
[exists] => 
[wasRecentlyCreated] => 
[attributes:protected] => Array
    (
    )

[original:protected] => Array
    (
    )

[changes:protected] => Array
    (
    )

[casts:protected] => Array
    (
    )

[dates:protected] => Array
    (
    )

[dateFormat:protected] => 
[appends:protected] => Array
    (
    )

[dispatchesEvents:protected] => Array
    (
    )

[observables:protected] => Array
    (
    )

[relations:protected] => Array
    (
    )

[touches:protected] => Array
    (
    )

[timestamps] => 1
[visible:protected] => Array
    (
    )

[fillable:protected] => Array
    (
    )

[guarded:protected] => Array
    (
        [0] => *
    )

)

Upvotes: 2

Views: 1994

Answers (1)

Rafid
Rafid

Reputation: 681

try to change axios.delete to axios.post, and add a _method field with the value delete in the data to be sent. like so

axios.post(`sms/${id}`, {params: {id: `${id}`}, _method: 'delete'})

Upvotes: 3

Related Questions