dimanche 6 août 2017

show method called instead of destroy method in laravel datatables

I'm having an issue with the laravel's datatables plugin, when I'm trying to delete an element from my datatables list it calls a show method, making the delete impossible. Can someone explain me why it behaves like that and how to fix it ? Here is my code.

Controller :

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);
}

View :

<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

Routes :

Route::any('project-data', 'Admin\ProjectsController@ajaxListing')->name('datatables.projectData');

Route::resource('projects', 'Admin\ProjectsController');



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2ueoCZY
via IFTTT

Aucun commentaire:

Enregistrer un commentaire