I am developing an application in PHP with Laravel and I have in my AlunosController and following method destroy:
public function destroy($id)
{
$aluno = new Aluno();
$result = $aluno->find($id)->delete();
if($result)
return redirect()->back();
else
return 'Falha';
}
So far so good, the function works correctly. The problem is that I decided to use SweetAlert to do my confirm before deletion. It looks like this:
var btnExcluir = $('.btn-danger');
btnExcluir.click(function(event){
event.preventDefault();
swal({
title: "Deseja continuar?",
text: "O cadastro do aluno será excluído permanentemente!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Sim, excluir!",
cancelButtonText: "Cancelar",
closeOnConfirm: false
}, function(){
$.ajax({
url: "/alunos/destroy",
type: "POST",
data: {
id: 8
},
dataType: "html",
success: function () {
swal("Excluído!", "O cadastro do aluno foi excluído com sucesso.", "success");
}, error: function(){
swal("Erro!", "Não foi possível remover o aluno.", "error");
}
});
});
});
I passed the fixed id 8 just for testing with a record that I have, but it is always falling into error.
I have tried to pass the id in the url, tried to use the type DELETE, tried to pass ID_ALUNO_ALU (which is the name of the attribute in the database and in the model) in date instead of id and nothing worked. Anyone who has ever used AJAX in Laravel could give me a light?
To get ahead, the part that interests my view is like this:
@foreach($alunos as $aluno)
<tr>
<td>{{$aluno['NM_NIS_ALU']}}</td>
<td>{{$aluno['ST_NOME_ALU']}}</td>
<td class="text-right">{{$aluno['IDADE']}}</td>
<td class="text-center">
{!! Form::open(['route' => ['alunos.destroy', $aluno['ID_ALUNO_ALU']], 'method' => 'delete', 'class' => 'form']) !!}
<button type="button" class="btn btn-warning btn-xs esconder-acao" data-toggle="modal" data-target="#modalAluno{{$aluno['ID_ALUNO_ALU']}}">Editar</button>
{!! Form::submit('Excluir', ['class' => 'btn btn-danger btn-xs esconder-acao']) !!}
{!! Form::close() !!}
</td>
</tr>
@endforeach
How do I pass the student id to js?
Thank you guys!