delete users in yajra laravel datatables -
hy, i'm new laravel using l5 , yajra/datatables plugin.when click on delete button gives error "tokenmismatchexception in verifycsrftoken.php line 53:" code is
controller.php
return datatables::of($users) ->addcolumn('action', function ($id) { return '<button class="btn-delete" data-remote="localhost/blog/public/delete/' . $id->id . '">delete</button>'; }) ->make(true);
in view
<script> $(function(){ $('#users-table').datatable({ processing: true, serverside: true, ajax: '{!! route("data") !!}', columns: [ {data: 'id', name: 'id'}, {data: 'name', name: 'name'}, {data: 'email', name: 'email'}, {data: 'created_at', name: 'created_at'}, {data: 'updated_at', name: 'updated_at'}, {data: 'action', name: 'action', orderable: false, searchable: false} ] }); $('#users-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 $.ajax({ url:'delete/{id}' , type: 'delete', datatype: 'json', data: {method: '_delete', submit: true} }).always(function (data) { $('#users-table').datatable().draw(false); }); }); }); </script>
in route.php
route::get('delete/{id}',array('as'=>'delete', 'uses'=>'editcontroller@delete'));
i take reference laravel yajra/datatables action delete not working
i tried
$('#users-table').on('click', '.btn-delete[data-remote]', function (e) { e.preventdefault(); var id = $(this).attr('id'); alert(id); $.ajaxsetup({
it gives "undefined"
its not working me..please help..
you need add following in html:
<meta name="csrf-token" content="{{ csrf_token() }}">
you should take @ page understand how works: https://laravel.com/docs/5.2/routing#csrf-protection
Comments
Post a Comment