【问题标题】:show method called instead of destroy method in laravel datatables在 laravel 数据表中调用 show 方法而不是 destroy 方法
【发布时间】:2017-08-06 19:31:10
【问题描述】:

我在使用 laravel 的数据表插件时遇到问题,当我试图从我的数据表列表中删除一个元素时,它会调用一个 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_ &eacute;l&eacute;ments",
                info: "Affichage de l'&eacute;lement _START_ &agrave; _END_ sur _TOTAL_ &eacute;l&eacute;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');

【问题讨论】:

    标签: php laravel laravel-5 datatables


    【解决方案1】:

    我终于找到了答案,问题出在路由上,我们需要在资源路由中添加一个except,并创建我们自己的删除路由,像这样:

    Route::get('projects/{project}/delete', 'Admin\ProjectsController@destroy')->name('projects.destroy');
    Route::resource('projects', 'Admin\ProjectsController', ['except' => 'destroy']);
    

    这样我们只需要在视图中的链接上调用路由即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-06
      • 1970-01-01
      • 2021-04-12
      • 1970-01-01
      • 1970-01-01
      • 2016-08-07
      • 1970-01-01
      • 2023-02-25
      相关资源
      最近更新 更多