Laravel使用destroy方法删除条目

时间:2016-04-15 12:44:22

标签: php laravel-5.2

我有一个带有删除按钮的表单,我在控制台上看到它正在发送删除请求。

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

我的路线是:

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


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

我的方法销毁如下:

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

编辑: 我忘了提到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 个答案:

答案 0 :(得分:0)

虽然$.ajax({ ... type: 'delete' ... })应该有用,但您需要set up your response to accept the 'delete' method

在Laravel中执行PUT,PATCH和DELETE的替代和标准方法是method spoofing

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

你的JS可能看起来像:

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

$.ajax({
    url: solsoFormAction,
    type: 'post',
    data: {_method: 'delete', _token: csrf},
    ...
});