Skip to content
Icon

WARNING You're browsing the documentation for an upcoming version of Laravel DataTables. The documentation and features of this release are subject to change.

DataTable Buttons - Export

This guide covers the export buttons available in the main DataTables package. For queue-based exports with Livewire, see the Export Usage guide.

Note: These buttons use client-side or server-side export functionality with yajra/DataTables Buttons plugin. For large datasets, consider using the Queue Export Package instead.

Export Button Group

Enable the complete export button group (Excel, CSV, and PDF buttons) with a single setting:

namespace App\DataTables;
 
use App\Models\User;
use Yajra\DataTables\Html\Button;
use Yajra\DataTables\Services\DataTable;
 
class UsersDataTable extends DataTable
{
public function html()
{
return $this->builder()
->columns($this->getColumns())
->layout([
'topStart' => 'buttons',
'topEnd' => 'search',
'bottomStart' => 'info',
'bottomEnd' => 'paging',
])
->buttons([
Button::make('export'),
]);
}
}

Individual Export Buttons

Export as Excel

Enable only the Excel export button:

public function html()
{
return $this->builder()
->columns($this->getColumns())
->layout([
'topStart' => 'buttons',
'topEnd' => 'search',
'bottomStart' => 'info',
'bottomEnd' => 'paging',
])
->buttons([
Button::make('excel'),
]);
}

Export as CSV

Enable only the CSV export button:

public function html()
{
return $this->builder()
->columns($this->getColumns())
->layout([
'topStart' => 'buttons',
'topEnd' => 'search',
'bottomStart' => 'info',
'bottomEnd' => 'paging',
])
->buttons([
Button::make('csv'),
]);
}

Export as PDF

Enable only the PDF export button:

public function html()
{
return $this->builder()
->columns($this->getColumns())
->layout([
'topStart' => 'buttons',
'topEnd' => 'search',
'bottomStart' => 'info',
'bottomEnd' => 'paging',
])
->buttons([
Button::make('pdf'),
]);
}

POST Method Export

For large datasets or when using Internet Explorer, use POST method exports to avoid URL length limitations:

public function html()
{
return $this->builder()
->columns($this->getColumns())
->layout([
'topStart' => 'buttons',
'topEnd' => 'search',
'bottomStart' => 'info',
'bottomEnd' => 'paging',
])
->buttons([
Button::make('postExcel'),
Button::make('postCsv'),
Button::make('postPdf'),
]);
}

Required Route Setup

Add a POST route for the export endpoint in your routes file:

use App\Http\Controllers\DataTableController;
 
// GET route for initial page load
Route::resource('sample', DataTableController::class);
 
// POST route for export functionality
Route::post('sample/export', [DataTableController::class, 'index'])
->name('sample.export');

Available Button Options

Button Description
export Export button group (Excel, CSV, PDF)
excel Export to Excel format
csv Export to CSV format
pdf Export to PDF format
postExcel Export to Excel using POST
postCsv Export to CSV using POST
postPdf Export to PDF using POST

Utility Buttons

Print Button

Enable print preview functionality:

public function html()
{
return $this->builder()
->columns($this->getColumns())
->layout([
'topStart' => 'buttons',
'topEnd' => 'search',
'bottomStart' => 'info',
'bottomEnd' => 'paging',
])
->buttons([
Button::make('print'),
]);
}

Reset Button

Reset the table to its original state:

public function html()
{
return $this->builder()
->columns($this->getColumns())
->layout([
'topStart' => 'buttons',
'topEnd' => 'search',
'bottomStart' => 'info',
'bottomEnd' => 'paging',
])
->buttons([
Button::make('reset'),
]);
}

Reload Button

Reload the table data without resetting filters:

public function html()
{
return $this->builder()
->columns($this->getColumns())
->layout([
'topStart' => 'buttons',
'topEnd' => 'search',
'bottomStart' => 'info',
'bottomEnd' => 'paging',
])
->buttons([
Button::make('reload'),
]);
}

Complete Button Configuration Example

public function html()
{
return $this->builder()
->columns($this->getColumns())
->layout([
'topStart' => 'buttons',
'topEnd' => 'search',
'bottomStart' => 'info',
'bottomEnd' => 'paging',
])
->buttons([
Button::make('export'),
Button::make('print'),
Button::make('reset'),
Button::make('reload'),
]);
}

Buttons Extension Configuration

For advanced button customization, use the fluent buttons method with extended configuration:

public function html()
{
return $this->builder()
->columns($this->getColumns())
->layout([
'topStart' => 'buttons',
'topEnd' => 'search',
'bottomStart' => 'info',
'bottomEnd' => 'paging',
])
->buttons([
Button::make('collection')
->text('<i class="fa fa-download"></i> Export')
->buttons([
Button::make('excel')
->text('Export to Excel')
->title('Users Report'),
Button::make('csv')
->text('Export to CSV'),
Button::make('pdf')
->text('Export to PDF')
->orientation('landscape'),
]),
Button::make('print'),
Button::make('reset'),
Button::make('reload'),
]);
}

Customize Export Columns

You can control which columns are included in exports using the $exportColumns property:

namespace App\DataTables;
 
use App\Models\User;
use Yajra\DataTables\Services\DataTable;
 
class UsersDataTable extends DataTable
{
protected string|array $exportColumns = ['name', 'email', 'created_at'];
}

You can also use '*' to include all columns from the HTML builder:

protected string|array $exportColumns = '*';

Exclude Columns from Export

Use $excludeFromExport to exclude specific columns from all exports:

namespace App\DataTables;
 
use App\Models\User;
use Yajra\DataTables\Services\DataTable;
 
class UsersDataTable extends DataTable
{
protected array $excludeFromExport = ['password', 'remember_token'];
}

Exclude Columns from Print

Use $excludeFromPrint to exclude specific columns from the print preview:

protected array $excludeFromPrint = ['password', 'action'];

Customize Print Columns

Use $printColumns to specify which columns to include in print preview:

protected string|array $printColumns = ['name', 'email'];

Custom Print Preview View

Override the default print preview blade template:

namespace App\DataTables;
 
use Yajra\DataTables\Services\DataTable;
 
class UsersDataTable extends DataTable
{
protected string $printPreview = 'datatables::custom-print';
}

Custom Export Filename

Set a custom filename for exports:

namespace App\DataTables;
 
use App\Models\User;
use Yajra\DataTables\Services\DataTable;
 
class UsersDataTable extends DataTable
{
protected string $filename = 'users_report';
 
// Or use the setter method
public function setFilename(string $filename): static
{
$this->filename = $filename;
 
return $this;
}
}

Customize Writer Type

You can customize the writer type for CSV, Excel, and PDF exports:

namespace App\DataTables;
 
use Yajra\DataTables\Services\DataTable;
 
class UsersDataTable extends DataTable
{
protected string $csvWriter = 'Csv';
protected string $excelWriter = 'Xlsx';
protected string $pdfWriter = 'Dompdf';
}

Available CSV writers: Csv Available Excel writers: Xlsx, Xls, Csv, Html Available PDF writers: Dompdf, Mpdf, Tcpdf, Snappy

Comparison: Buttons vs Queue Export

Feature Buttons Export Queue Export
Dataset size Small to medium Large
Processing Synchronous Asynchronous
Timeout risk High for large data None
User experience Immediate download Background job + polling
Server load High during export Distributed over time
Setup complexity Simple Requires queue setup

See Also