我现在正在使用Laravel 5.4和Yajra Datatables插件。一切正常,但删除不起作用。我的网址是这样的...... http://localhost:8000/admin/view/%7B%7B%20route(' admin.faculty.destroy',%20 $ faculties-%3Eid)%20%7D%7D
这是我的控制器:
hasSubscription
这是我的js
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);
}
编辑工作正常
答案 0 :(得分:1)
你可以尝试这个。只需删除表单并将其替换为此代码
<a href="'. route('admin.faculty.destroy', $faculties->id) .'" class="btn btn-xs btn-danger"><i class="glyphicon glyphicon-trash"></i> Delete</a>
你的api控制器必须是这样的。
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);
}
答案 1 :(得分:0)
我已经解决了。在控制器中我只使用<button class="btn btn-xs btn-danger btn-delete" data-remote="/admin/faculty/' . $faculties->id . '"><i class="glyphicon glyphicon-trash"></i>Delete</button>
而不是形式。并在视图页面上使用ajax调用
$('#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!");
});
答案 2 :(得分:0)
它在xampp和专用主机上工作,但是在linux共享主机中,这种方法不起作用 1-将类型从删除更改为发布 2-将方法更改为_method 3-将_DELETE更改为DELETE 下面的代码为我工作
$('#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!");});