Laravel AJAX Delete route

0

Working with Laravel 5.5 I have a problem with the variable "id" when placing the AJAX route. I happened to show you

Route

Route::DELETE('deletePlan/{id}', 'FormDController@deletePlan')->name('d.deletePlan');

In the AJAX I have the following (I clarify that the value of the "id" to the AJAX arrives well, the error arises when I add it to the route

$('.delete_plan').on('click', function(e) {
e.preventDefault();
var row = $(this).parents(".contenedor");
var li = e.target.parentNode;
var id= li.id;
var param = {
    'id'       : id,
    '_method'  : 'delete',
    '_token'   : '{{Session::token()}}'
};
ruta1 = "http://192.168.156.201/EMPRESA/intranet/administrator/d/deletePlan/" + id;
$.ajax({
    url: ruta1,
    type : 'post',
    data :   param,
    success: function( msg ) {
        row.fadeOut();
        alert("Registro borrado correctamente !!!");
    },
    error: function( data ) {
        alert("Ocurrió un error !!!");
    }
});

return false;

});

As it is route1 it works well, obviously I put it to test only, this means that the rest is all good, and in the controller is also all good. Now instead of that url probe in different ways put the "router" as it should, and in some cases it pulls me that does not know id, in other cases it happens to the controller the WORD id and not its value, etc. I could not find the way to put the "route" accordingly.

"{{ route('d.deletePlan', "id") }}";

"{{ route('d.deletePlan', 'id') }}";

"{{ route('discapacidad.deletePlan') }}", + id;

None of these forms works for me, they all put the corresponding url but the variable in the first case and in the second one it puts the word "id" instead of taking the corresponding number. And in the last option I get an error saying that the parameter is missing

Could you help me how the route is placed? Also try to put single quotes on the outside instead of double quotes. I have tried several options but I could not make it work

I reiterate, the route or the URL places it well, the theme is the parameter, I can not form the url, for example

"http://192.168.156.201/EMPRESA/intranet/administrator/d/deletePlan/5";
    
asked by desarrollosTELLO 09.02.2018 в 02:09
source

2 answers

0

Using the name of the route:

"{{ route('d.deletePlan') }}"+ "/" + id;

You have to put the bar between the route and the id because if you do not do it you are putting an incorrect route.

    
answered by 09.02.2018 в 10:22
0

A practical and simple solution, so as not to complicate the use of route() with variables that are later added by JavaScript, and taking into account that the parameter of the route is required, is to use a string as a value of "id" and then replace it also from JavaScript:

let id = li.id

let ruta1 = "{{ route('d.deletePlan', 'req_id') }}"

var ruta = ruta1.replace('req_id', id)

$.ajax({
    url: ruta,
    ...
    
answered by 10.02.2018 в 04:21