1
I am developing an application in PHP with Laravel and I have in my Alunoscontroller and following method:
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 make my confirm before deletion. It looked 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 I have but it is always falling into error.
I tried to pass the id in the url, tried to use type DELETE, tried to pass ID_ALUNO_ALU (which is the name of the attribute in the database and model) in date instead of id and nothing worked. Someone who’s used AJAX on Laravel could give me a light?
In advance, the interesting part of 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 student id to js?
Thank you guys!
Try changing dataType: "html" to dataType: "JSON"
– Matheus Fidelis
"but it’s always falling into error" that in Javascript, right? And in PHP, what happens? The record is properly deleted from the database?
– Woss
Do not delete no. I found that the date is not coming in Stroy, but still do not know why
– Matheus