Problem with destroy in laravel

0

I have a destroy method in my controller and I call it from a view to delete a record, but after confirming that I want to delete, the window remains blank and does not erase anything at all and does not redirect me to another view.

The driver code is this:

    public function destroy($id)
    {
       Estudiante::findOrFail($id)->delete();
       return redirect('estudiante');
    }

And in the view I call it with the following code:

<a href='{{ route('estudiante.destroy',$estudiante->id)}}' onclick="return confirm('¿Esta seguro de eliminar este Estudiante?')" data-toggle="tooltip" data-placement="right" title="Eliminar">
    <i class="fas fa-trash-alt" aria-hidden="true"></i>
</a>

Am I doing something wrong or something is missing?

------------ Fixed -------------

Some friends at the university gave me this solution and I want to share it with anyone who needs it.

It was just to create a new route in the web.php file

Route::get('estudiante/ocultar/{id}', 'EstudianteController@ocultar');

Then create a new function in the controller to which you are going to point the new route, and place what was in the destroy function.

public function ocultar($id)
{
    Estudiante::findOrFail($id)->delete();
    return redirect('estudiante');
}

And finally change the link in the following way:

<a href='{{ url('estudiante/ocultar',$estudiante->id)}}' onclick="return confirm('¿Esta seguro de eliminar este Estudiante?')" data-toggle="tooltip" data-placement="right" title="Eliminar">
     <i class="fas fa-trash-alt" aria-hidden="true"></i>
</a>

Done all this works quietly, thank you very much everyone for the tips

    
asked by user85131 09.05.2018 в 02:52
source

2 answers

0

Of course, clicking on the link will go through the GET method, which ends up executing the SHOW method of your control

How to fix it?

The easiest is with a Form (FORM). Personally I would create a Modal where my form will be that arrives at the method DESTROY

<div id="modal-emergent" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <form role='form' method="POST">
        @csrf
        @method('DELETE')
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <h5 id="myModalLabel">Aviso</h5>
        </div>
        <div class="modal-body">
            <h5 style="text-align: center">Desea realmente eliminar el estudiante?</h5>
        </div>
        <div class="modal-footer">
            <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">Cancelar</button>
            <button class="btn btn-danger">Eliminar</button>
        </div>
    </form>
</div>

If you notice I have a hidden input that will contain the ID, so that I get the ID until I put it with a little script, what you do is wait for a button to delete click to place a paramenter that said button will have, that would be the route and place it in the action of the FORM

<script type="text/javascript">
    $('a').click(function(event) {
        var data_url = $(this).attr('data-url');
        $('#modal-emergent form').attr('action', data_url);
    });
</script>

My delete button would be

<a data-toggle="modal" data-target="#modal-emergent" data-url="{{route('reading.destroy', $reading->id)}}" class="btn btn-danger tip-top" title="Eliminar lectura">
    <i class="glyphicon glyphicon-remove"></i>
</a>
    
answered by 09.05.2018 в 03:20
-1

When you want to delete something in Laravel by means of an HTTP request, you must use the HTTP verb "DELETE", however, browsers can not normally send a request of this type, so you must do a POST or a GET , and include a form with the appropriate verb in the field _method .

If you use Laravel Collective (which I think is the simplest), it would be like this:

{!! Form::open(['route' => ['estudiante.destroy', $estudiante->id], 'method' => 'DELETE']) !!} 
    {!! Form::submit('Eliminar', ['class' => 'button']) !!}
{!! Form::close() !!}

You can also put the form aside and submit the respective submit with JavaScript, or include modal, alert, etc.

    
answered by 09.05.2018 в 15:20