yajra / laravel-datatables

jQuery DataTables API for Laravel
https://yajrabox.com/docs/laravel-datatables
MIT License
4.76k stars 858 forks source link

Receive Forbidden - Error for export functions #1051

Closed ngsource closed 7 years ago

ngsource commented 7 years ago

I have installed all the dependencies correctly. When I click on the export button I receive Forbidden - Error. Below is my code. 403 forbidden

` namespace App\DataTables;

use App\User; use Yajra\Datatables\Services\DataTable;

class UserDataTable extends DataTable {

/**
 * Display ajax response.
 *
 * @return \Illuminate\Http\JsonResponse
 */
public function ajax()
{
    return $this->datatables
        ->eloquent($this->query())
        ->make(true);
}

/**
 * Build DataTable class.
 *
 * @return \Yajra\Datatables\Engines\BaseEngine
 */
public function dataTable()
{
    return $this->datatables
        ->eloquent($this->query())
        ->addColumn('action', 'path.to.action.view');
}

/**
 * Get the query object to be processed by dataTables.
 *
 * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|\Illuminate\Support\Collection
 */
public function query()
{
    $query = User::query();
    return $this->applyScopes($query);
}

/**
 * Optional method if you want to use html builder.
 *
 * @return \Yajra\Datatables\Html\Builder
 */
public function html()
{
    return $this->builder()
        ->columns($this->getColumns())
        ->parameters([
            'dom'          => 'Bfrtip',
            'buttons'      => ['export', 'print', 'reset', 'reload'],
        ]);
}

/**
 * Get columns.
 *
 * @return array
 */
protected function getColumns()
{
    return [
        'id',
        'email',
        'name',
        'created_at',
        'updated_at',
    ];
}

/**
 * Get filename for export.
 *
 * @return string
 */
protected function filename()
{
    return 'userdatatable_' . time();
}

}

return $dataTable->render('app.clients.index');

{!! $dataTable->table() !!}

@push('scripts')

    <link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.0.3/css/buttons.dataTables.min.css">
    <script src="https://cdn.datatables.net/buttons/1.2.4/js/dataTables.buttons.min.js"></script>
    <script src=" {{asset('vendor/datatables/buttons.server-side.js')}}"></script>
    {!! $dataTable->scripts() !!}
@endpush`
yajra commented 7 years ago

Were you able to install dompdf: ~6.2.0 or laravel-snappy package?

yajra commented 7 years ago

Some docs for ref: https://yajrabox.com/docs/laravel-datatables/7.0/buttons-config#pdf-generator

tad3j commented 7 years ago

I'm having the same issue. I have these installed:

"barryvdh/laravel-snappy": "^0.3.1",

"dompdf/dompdf": "^0.7.0",

And under datatables-buttons.php I have this configuration:

'pdf_generator' => 'snappy',

I'm getting Forbidden on all exports, CSV, Excel and PDF.

Seems like the request is issued to wrong route, since I see that the URL in URL bar is:

http://website.dev/&draw=1&columns%5B0%5D%5Bdata%5D=name . . . &action=excel

...notice that request is sent to index page (instead of the resource) and that query part is malformed. All DataTables exporters redirect to this URL.

It used to work, I only noticed today that it doesn't.

UPDATE 1: Tried to downgrade dompdf to ~0.6.1 (according to docs), but that doesn't change anything.

SOLUTION: Copying (publishing) buttons.server-side.js from vendor folder to my assets folder (+ running gulp) solved the issue for me. Seems like we were using old buttons JS code...

yajra commented 7 years ago

@tad3j yeah, there was a bug on buttons-server-side.js and was fixed on the latest release. Thanks!