lundi 21 novembre 2016

Laravel5 ajax delete wouldn't work

I'm working on a project, where I should delete users from my users table with ajax. I've been seeking for multiple solutions, but it just simply wouldn't work anyhow, I get a "Failed to load resource: the server responded with a status of 500 (Internal Server Error)".

The JS:

$('.btn-delete').click(function(){
            var id = $(this).val();
            $.ajax({

                type: 'DELETE',
                url: '/laravel-exercise/public/index/'+id,
                success: function (data) {
                    console.log('Success:', data);
                },
                error: function (data) {
                    console.log('Error:', data);
                }
            });
        });

The view:

<button class="btn btn-danger btn-delete" value="" data-token="">Delete</button>

The route:

Route::delete('index/{$id}', 'UsersController@destroy');

The destroy():

public function destroy($id)
        {
            $user = User::findOrFail($id);
            $user->delete();
            
            return view('pages.index')->with([
                'flash_message' => 'The user has been deleted.',
                'flash_message_important' => 'true',
            ]);
        }

What could go wrong here? Thanks in advance!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire