4
votes

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;
    });     
1
How have you defined the routes for the controller, is it a resource controller or an implicit controller? Can you include your routes.php code as well.Ahmad Baktash Hayeri
It is a resource controller.. Does that make any difference?ledesma
If you had it as an implicit controller, it would. Anyway, can you post your controller and routes.php full code to the question?Ahmad Baktash Hayeri
Sorry, It is an AJAX request. I just edited the question...ledesma
I understand that its an AJAX request, but you must have defined the route for the delete request that you're making (most probably in the file routes.php), so can you post its content as well as full controller code for the destroy method?Ahmad Baktash Hayeri

1 Answers

0
votes

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},
    ...
});