Shehan L Jayasinghe
Shehan L Jayasinghe

Reputation: 61

DELETE not working in the Laravel Datatable

I am working with Laravel Datatable and I have stuck in one point. Below code for delete the entry in the TagManagement model. But it isn't delete the entry and worse thing is it didn't show any error. Can anybody find the error in below code?

view

 $(document.body).on("click",".remove-tag", function () {

        var tag_id = $(this).data('id');
        showConfirm("DELETE", "Do you want to delete this Tag ?","deleteTag("+tag_id+")");
    });

    function deleteTag(id){
        $.ajax({
            type: 'get',
            url: '{!! url('delete-tag') !!}',
            data: {tag_id: id},
            success: function (data) {
                    if (data == "SUCCESS") {
                        $('[data-id="' + id + '"]').closest('tr').remove();
                        showAlert("SUCCESS","Delete Tag successful");
                    }
                }, error: function (data) {

                    showAlert("FAIL","Delete Tag fail");
                }

        });
    }
        var tag_id = $(this).data('id');
        showConfirm("DELETE", "Do you want to delete this Tag ?","deleteTag("+tag_id+")");
    });

    function deleteTag(id){
        $.ajax({
            type: 'get',
            url: '{!! url('delete-tag') !!}',
            data: {tag_id: id},
            success: function (data) {
                    if (data == "SUCCESS") {
                        $('[data-id="' + id + '"]').closest('tr').remove();
                        showAlert("SUCCESS","Delete Tag successful");
                    }
                }, error: function (data) {

                    showAlert("FAIL","Delete Tag fail");
                }

        });
    }

Controller

public function destroy($id)
    {

        $tagManagement = TagManagement::find($id);
        $deleted = $tagManagement->delete();

        if ($deleted) {
            return "SUCCESS";
        } else {
            return "FAIL";
        }
    }

public function loadTags()
{

    $Tags = TagManagement::all();

    return DataTables::of($Tags)
        ->addColumn('action', function ($tag) {
            return '<a href="' . url('/tags/' . $tag->id . '/edit') . '" class="btn btn-default edit_btn_styles" data-toggle="tooltip" data-placement="top" title="Edit"><i class="fa fa-wrench" aria-hidden="true"></i></a>
                    <button type="button" data-id="' . $tag->id . '" class="btn btn-default remove-tag remove-btn" data-toggle="tooltip" data-placement="top" title="Delete"><i class="fas fa-trash-alt" aria-hidden="true"></i></button>';
        })
        ->rawColumns(['action'])
        ->make(true);
}

}

**Route**

Route::get('/delete-tag', 'AdminPanel\TagController@destroy');


Upvotes: 0

Views: 1469

Answers (2)

Md Abdul Awal
Md Abdul Awal

Reputation: 522

It is not your Datatable problem, you missed some code, you did not define route & jQuery function properly, your destroy($id) function received a parameter but you do not receive any parameter in your route & you not send _token in your ajax action you need to send _token

Check My code I am edited your code.

//Change your Route
Route::get('delete-tag/{id}', 'AdminPanel\TagController@destroy')->name('deleteTag');

//Change your function
function deleteTag(id){
    $.ajax({
        type: "GET",
        dataType: 'JSON',
        url:'{{ route('deleteTag', '') }}/'+id,
        data: {_token: '{{csrf_token()}}'},
        success: function (data) {
            if (data == "SUCCESS") {
                $('[data-id="' + id + '"]').closest('tr').remove();
                showAlert("SUCCESS","Delete Tag successful");
            }
        }, error: function (data) {

            showAlert("FAIL","Delete Tag fail");
        }
    });
}

Upvotes: 0

Nicolai Manev
Nicolai Manev

Reputation: 11

Your route and controller method don't seem to correspond. First of all, it is better to use the "delete" HTTP request method for delete actions, but this is not what is causing your problem.

You defined your route as /delete-tag but in your controller you expect an $id as a parameter to your destroy method. In order for that to work you would need to have the route like this /delete-tag/{id} and construct the URL for your ajax call correspondingly on the frontend. I'm surprised you don't get the Missing argument 1 for App\Providers\RouteServiceProvider::{closure}() exception for malforming your request this way.

Laravel documentation explains very well how to define routes with parameters.

It would be helpful if you included Laravel version in your question.

Here is how it should work:

Route definition

Route::delete('/delete-tag/{id}', 'AdminPanel\TagController@destroy')->name('delete-tag');

JS function

function deleteTag(id){
        let route = '{!! route('delete-tag', ['id' => '%id%']) !!}';
        $.ajax({
            type: 'post',
            url: route.replace('%id%', id);,
            data: {_method: 'delete'},
            success: function (data) {
                    if (data == "SUCCESS") {
                        $('[data-id="' + id + '"]').closest('tr').remove();
                        showAlert("SUCCESS","Delete Tag successful");
                    }
                }, error: function (data) {

                    showAlert("FAIL","Delete Tag fail");
                }

        });
    }

Upvotes: 1

Related Questions