0

I am trying to generate CSV, excel and pdf files from the data in data tables.

My code is as following-

In web.php

Route::post('/admin/server', 'ExpenseController@exporter');

In ExpenseController

public function exporter(){


    $table = 'products';


    $primaryKey = 'id';


    $columns = array(
    array( 'db' => 'product', 'dt' => 0 ),
    array( 'db' => 'vendor', 'dt' => 1 ),
    array( 'db' => 'colors', 'dt' => 2 ),
    array( 'db' => 'sizes', 'dt' => 2 ),
    array( 'db' => 'amount paid', 'dt' => 2 ),
    array( 'db' => 'expected profit', 'dt' => 2 )
    );


    $sql_details = array(
    'user' => 'root',
    'pass' => '',
    'db' => 'ecom',
    'host' => 'localhost'
    );


    require( 'vendor/DataTables/server-side/scripts/ssp.class.php' );

    echo json_encode(
    SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
    );
}

And, finally in the script tag

$(document).ready(function () {
var table = $('#exporter-table').DataTable({
    "paging": true,
    "processing": true,
    "serverSide": true,
    'serverMethod': 'POST',
    "ajax": "/admin/server",
    "data": {
        "_token": "{{ csrf_token() }}"
    },
    dom: 'Bfrtip',
    buttons: ['csv', 'excel', 'pdf']
}
);
});

Every time I load the page I get the error of CSRF token mismatched even if the token is correct.

What should be done?

Nafeez Quraishi
  • 5,380
  • 2
  • 27
  • 34

1 Answers1

3

Please Try this:

 'data': function (d) {
                d._token = $("input[name=_token]").val();

            }
PHP Geek
  • 3,949
  • 1
  • 16
  • 32