1

I am now using Laravel 5.4 and Yajra Datatables plugin. Everything work fine but delete doesn't work. My url goes like this... http://localhost:8000/admin/view/%7B%7B%20route('admin.faculty.destroy',%20$faculties-%3Eid)%20%7D%7D

Here is my controller :

public function getFacultiesData()
{
    $faculties = faculty::select(['id','name','designation','email','mobile', 'updated_at']);
    return Datatables::of($faculties)
    ->addColumn('action', function ($faculties) {
            return '<a href="/admin/faculty/'.$faculties->id.'/edit" class="btn btn-xs btn-primary"><i class="glyphicon glyphicon-edit"></i> Edit</a>
                <form class="form-group" action="{{ route(\'admin.faculty.destroy\', $faculties->id) }}" method="POST">
                    <input type="hidden" name="_method" value="DELETE">
                    <button class="btn btn-xs btn-danger"><i class="glyphicon glyphicon-trash"></i>Delete</button>
                </form>    ';})
        ->editColumn('updated_at', function ($faculties) {
            return $faculties->updated_at->diffForHumans();
        })
        ->filterColumn('updated_at', function ($query, $keyword) {
            $query->whereRaw("DATE_FORMAT(updated_at,'%Y/%m/%d') like ?", ["%$keyword%"]);
        })
        ->make(true);
}

Here Is my js

$(function() {
$('#faculties-table').DataTable({
    processing: false,
    serverSide: true,
    ajax:'http://localhost:8000/admin/get/faculties/data',
    columns: [
    {data: 'name'},
    {data: 'designation'},
    {data: 'email'},
    {data: 'mobile'},
    {data: 'updated_at'},
    {data: 'action', name: 'action', orderable: false, searchable: false}
    ]});
});

Edit is working perfect

3 Answers 3

3

I have solved it. In controller I just use <button class="btn btn-xs btn-danger btn-delete" data-remote="/admin/faculty/' . $faculties->id . '"><i class="glyphicon glyphicon-trash"></i>Delete</button> instead of form. And use ajax call on view page

$('#faculties-table').on('click', '.btn-delete[data-remote]', function (e) { 
    e.preventDefault();
     $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    var url = $(this).data('remote');
    // confirm then
    if (confirm('Are you sure you want to delete this?')) {
        $.ajax({
            url: url,
            type: 'DELETE',
            dataType: 'json',
            data: {method: '_DELETE', submit: true}
        }).always(function (data) {
            $('#faculties-table').DataTable().draw(false);
        });
    }else
        alert("You have cancelled!");
});
Sign up to request clarification or add additional context in comments.

Comments

2

You can try this one. just remove Form and replace it with this code

<a href="'. route('admin.faculty.destroy', $faculties->id) .'" class="btn btn-xs btn-danger"><i class="glyphicon glyphicon-trash"></i> Delete</a>

Your api controller must be like this.

 public function getFacultiesData()
    {
        $faculties = faculty::select(['id','name','designation','email','mobile', 'updated_at']);
        return Datatables::of($faculties)
        ->addColumn('action', function ($faculties) {
                return '<a href="/admin/faculty/'.$faculties->id.'/edit" class="btn btn-xs btn-primary"><i class="glyphicon glyphicon-edit"></i> Edit</a>
                   <a href="'. route('admin.faculty.destroy', $faculties->id) .'" class="btn btn-xs btn-danger"><i class="glyphicon glyphicon-trash"></i> Delete</a>   ';
        })
        ->editColumn('updated_at', function ($faculties) {
            return $faculties->updated_at->diffForHumans();
        })
        ->filterColumn('updated_at', function ($query, $keyword) {
            $query->whereRaw("DATE_FORMAT(updated_at,'%Y/%m/%d') like ?", ["%$keyword%"]);
        })
        ->make(true);
    }

Comments

0

based on @Shaikhul-Saad answer

it's work on xampp and dedicate host but in linux shared hosting this approach is not working 1- change type from delete to post 2- change method to _method 3- change _DELETE to DELETE below code is working for me

$('#faculties-table').on('click', '.btn-delete[data-remote]', function (e) { 
e.preventDefault();
 $.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});
var url = $(this).data('remote');
// confirm then
if (confirm('Are you sure you want to delete this?')) {
    $.ajax({
        url: url,
        type: 'POST',
        dataType: 'json',
        data: {_method: 'DELETE'}
    }).always(function (data) {
        $('#faculties-table').DataTable().draw(false);
    });
}else
    alert("You have cancelled!");});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.