-1

In a Laravel project I want to delete an item using ajax. My route is

Route::resource('title','TitleController');

Here is my ajax code

$.ajax({
        url:'title',
        type: 'post',
        data: {_method: 'delete', _token :token, id : id},
        success:function(msg){

            console.log(msg);

         }
      });

So how to use DELETE method in a ajax with a parameter for deleting items ?

B. Desai
  • 16,414
  • 5
  • 26
  • 47
Arafat
  • 143
  • 1
  • 4
  • 14
  • Possible duplicate of [How to send a PUT/DELETE request in jQuery?](https://stackoverflow.com/questions/2153917/how-to-send-a-put-delete-request-in-jquery) – Mark Nov 10 '17 at 05:00
  • `type: "DELETE"` instead of POST should work without the need for the `_method` field. – apokryfos Nov 10 '17 at 11:10
  • Right apokryfos. I updated my solved code :) – Arafat Nov 10 '17 at 12:50

2 Answers2

0

Use the correct route name,pass the id in the url

 url:{{route('title.destroy',['id'=>$title->id]}},

or

url:{{action('TitleController@destroy',['id'=>$title->id])}}

in your controller you do something similar:

public function destroy($id)
    {
       Title:::find($id)->delete();
       redirect()->to('/')->with('message','Successfully deleted the title');
    }

As you have the id in javascript you will need to concatenate it to the url

full code:

$.ajax({
        url:'{{url('title')}}'+id,
        type: 'post',
        data: {_method: 'delete', _token :token},
        success:function(msg){

            console.log(msg);

         }
      });
madalinivascu
  • 32,064
  • 4
  • 39
  • 55
  • When I use 'resource' in route, laravel automatically call the function according to the method and parameter.... So I don't need to use the specific function name in the URL if I can send the method accurately.... I want to know... Is any process possible in Ajax ? #madalinivascu – Arafat Nov 10 '17 at 05:56
  • @Arafat what do you mean by that? – madalinivascu Nov 10 '17 at 05:59
  • can you post your controller? – madalinivascu Nov 10 '17 at 06:01
  • the ajax you are doing right now is passing the id as a request param, the delete route is `title/{id}` not `title` – madalinivascu Nov 10 '17 at 06:13
  • ya....but like post or get method we need delete method also for deleting an item ...right ? – Arafat Nov 10 '17 at 06:13
  • some server are not capable of doing a DELETE request so you do a POST request with a request param `_method` of `delete` as you are doing right now – madalinivascu Nov 10 '17 at 06:17
  • by **delete method** i think you mean controller function, yes you will need a controller function named destroy that takes a id as a param – madalinivascu Nov 10 '17 at 06:19
  • no .. I mean DELETE request.....But i am confused that whenever the url is "title/{id}" with DELETE request then I don't need to specify the method name "destroy"...Laravel auto find the method as the request type is DELETE. But in this " url:{{route('title.destroy',['id'=>$title->id]}}, " I need to provide the method name too – Arafat Nov 10 '17 at 06:34
  • `title.destroy` is **not** the method name is the route name – madalinivascu Nov 10 '17 at 06:39
  • sorry...ya route name. do u understand what i mean now? – Arafat Nov 10 '17 at 06:42
  • you don't need to use the route name if you don't want, but you will need your url as `title/{id}` and your request param `_method` of `delete` or you will do a update request ,without the `{id}` param and the `_method` param you will do a create request – madalinivascu Nov 10 '17 at 06:42
  • send it where?? – madalinivascu Nov 10 '17 at 06:50
  • #madalinivascu thanks for your help...I have updated the solved code. Thanks a lot :) – Arafat Nov 10 '17 at 06:56
  • don't forget the green check, get read of # your not on instagram or twitter – madalinivascu Nov 10 '17 at 07:01
0

Here is the solved ajax code

$.ajax({
        url:"{{url('title/" +id+ "')}}", //----- OR url :"/title/"+id,
        type: 'DELETE',
        data: {_token :token},
        success:function(msg){
            console.log(msg);
            }   
       });
Arafat
  • 143
  • 1
  • 4
  • 14
  • i suggest you use `url:'{{url('title')}}'+id,` from my answer, your url is relative to the current route, if you alter the current route your ajax will be broken searching for a row relative to the new route – madalinivascu Nov 10 '17 at 07:12
  • url : `"{{url('title/" +id+ "')}}"` – Arafat Nov 10 '17 at 12:47