To remove only one record I realize this ajax
var borrar = confirm("¿Realmente desea eliminarlo?");
if (borrar)
{
var token = document.getElementById('token').value;
$.ajax({
headers: {'X-CSRF-TOKEN': token},
dataType: "json",
data: {radicado: radicado},
url: ip+'/eliminarRadicado/delete/'+radicado,
type: 'get',
beforeSend: function(){
},
success: function(respuesta){
alert(respuesta);
},
error: function(xhr,err){
alert("Error");
}
});
}
which sends by $get the id of the record that I delete the file and run this route :
Route::get('eliminarRadicado/delete/{id}', 'RadicadoController@destroy');
which ultimately goes to the driver and performs the function of removing
public function destroy($id)
{
\App\Radicado::destroy($id);
return response()->json(['Documento eliminado']);
}
What is not like removing more than one record (id ) I send. Any ideas ?
You shouldn't send delete requests using a GET
verb. Instead you should use the DELETE
verb which is semantically correct.
With your current approach, sending the X-CSRF-TOKEN
header doesn't do anything, as Laravel doesn't check the CSRF token for read requests ( GET
and HEAD
). Instead you should pass all the IDs you want to deleted as parameters with a DELETE
request like so:
var ids = [1, 10, 17]; // Use your own logic to set the array of the IDs here
$.ajax({
headers : {'X-CSRF-TOKEN': token },
dataType: "json",
data : { ids: ids }, // Pass IDs array
url : ip + '/eliminarRadicado/delete',
type : 'delete', // Send a delete request
beforeSend: function () {
},
success: function (respuesta) {
alert(respuesta);
},
error: function (xhr, err) {
alert("Error");
}
});
Then change your route definition to this:
Route::delete('eliminarRadicado/delete', 'RadicadoController@destroy');
And in your destroy
controller method use the array of IDs received via the request:
use Illuminate\Http\Request;
...
public function destroy(Request $request)
{
\App\Radicado::destroy($request->input('ids'));
return response()->json(['Documento eliminado']);
}
Now you can pass an array of one or more IDs to be deleted using the same request.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.