我对laravel的datatables插件有疑问,当我试图从我的数据表列表中删除一个元素时,它调用一个show方法,使得删除成为不可能。有人能解释一下为什么它会像那样,以及如何解决它?这是我的代码。
控制器:
public function destroy($project) {
$project = Project::find($project);
$project->delete();
session()->flash('message', 'projet supprimé');
return redirect()->back();
}
public function ajaxListing() {
$projects = Project::select(['id', 'title']);
return Datatables::of($projects)
->addColumn('action', function ($project) {
return '<a class="data-action" href="'.route('projects.edit', $project->id).'">
<i class="fa fa-pencil-square-o fa-2x" aria-hidden="true"></i></a>
<a class="data-action" href="'.route('projects.destroy', $project->id).'">
<i class="fa fa-times fa-2x" aria-hidden="true"></i></a>';
})
->make(true);
}
查看:
<table class="table table-bordered table-striped dataTable" id="listingProjects">
<thead>
<th>ID</th>
<th>Titre</th>
<th>Actions</th>
</thead>
</table>
@push('scripts')
<script>
$(document).ready(function () {
$('#listingProjects').DataTable({
processing: true,
serverSide: true,
ordering: true,
language: {
processing: "Traitement en cours...",
search: 'Recherche : ',
lengthMenu: "Afficher _MENU_ éléments",
info: "Affichage de l'élement _START_ à _END_ sur _TOTAL_ éléments",
paginate : {
first : '<i class="fa fa-fast-backward" aria-hidden="true"></i>',
previous : '<i class="fa fa-chevron-circle-left" aria-hidden="true"></i>',
next : '<i class="fa fa-chevron-circle-right" aria-hidden="true"></i>',
last: '<i class="fa fa-fast-forward" aria-hidden="true"></i>',
}
},
ajax: '{!! route('datatables.projectData') !!}',
columns: [
{data: 'id', name: 'id'},
{data: 'title', name: 'title'},
{data: 'action', name: 'action', orderable: false, searchable: false}
]
});
});
</script>
@endpush
路线:
Route::any('project-data', 'Admin\ProjectsController@ajaxListing')->name('datatables.projectData');
Route::resource('projects', 'Admin\ProjectsController');
答案 0 :(得分:1)
我终于找到了答案,问题出在路线上,我们需要在资源路线上添加一个除外地并创建我们自己的删除路线,如下:
Route::get('projects/{project}/delete', 'Admin\ProjectsController@destroy')->name('projects.destroy');
Route::resource('projects', 'Admin\ProjectsController', ['except' => 'destroy']);
这样我们只需要在视图中的链接上调用路由。