4

I have got a form with a delete button which I see on the console that it's sending a delete request.

jquery.js:4 DELETE http://laravel.com/painel/player/53 500 (Internal Server Error)

and my route is:

Route::resource('painel/player','PlayerController');


| DELETE    | painel/player/{player}      | painel.player.destroy | App\Http\Controllers\PlayerController@destroy    |

and my method destroy is as below:

    public function destroy($id)
    {
            $player = Player::where('id_player', '=', $id)->first();
            $player->delete();
            $player = array(
                    'users'         => Player::all(),
                    'refresh'       => true
            );
            return View::make('painel.player.show', $player);
    }

EDIT: I forgot to mention the ajax:

    $( document ).on('click', '.solsoConfirm', function(){
        $("#solsoDeletForm").prop('action', $(this).attr('data-href'));
    });

    $( document ).on('click', '.solsoDelete', function(e){
        e.preventDefault();

        var solsoSelector   = $(this);
        var solsoFormAction = $('#solsoDeletForm').attr('action');

        $.ajax({
            url:    solsoFormAction,
            type:   'delete',
            cache:  false,
            dataType: 'html',
            success:function(data) {
                $('#solsoDeleteModal').modal('hide');
                $('#ajaxTable').html(data);
                $('#countClients').text( $('.solsoTable').attr('data-all') );
                $.growl.notice({ title: solsoSelector.attr('data-message-title'), message: solsoSelector.attr('data-message-success') });
                $('.solsoTable').dataTable();
            }
        }); 

        return false;
    });     
ledesma
  • 248
  • 1
  • 7
  • 18

1 Answers1

0

While your $.ajax({ ... type: 'delete' ... }) should work, you need to set up your response to accept the 'delete' method.

The alternative and standard way to do PUT, PATCH, and DELETE in Laravel is via method spoofing:

<form action="painel/player/{{ $id }}" method="POST">
    {{ method_field('DELETE') }}
    {{ csrf_field() }}
</form>

Your JS might look something like:

var csrf = $('input[name="_token"]').val();

$.ajax({
    url: solsoFormAction,
    type: 'post',
    data: {_method: 'delete', _token: csrf},
    ...
}); 
Community
  • 1
  • 1
Don't Panic
  • 13,965
  • 5
  • 32
  • 51
  • BTW the actual content of the 500 error will probably tell you where the problem is - you should be able to see it in your browser's developer tools. – Don't Panic Apr 16 '16 at 12:08
  • It's not working... the dev tool only says "500 (Internal Server Error)".. it says the error is on the ajax method.. if I send the request without being an ajax request.. it works. – ledesma Apr 18 '16 at 11:54
  • In dev tools, switch to network tab, and click the POST. You should see "Headers", and "Response", and some other things. Click Response - you should see the full response that Laravel generates, which will describe the problem. If you can't find it, check the Laravel logfile. Have you updated your AJAX code to send both _method and _token? – Don't Panic Apr 18 '16 at 14:46