4,000 emails/month for free | Mailtrap sends real emails now!

Laravel Datatables Export

yajra/laravel-datatables-export image

Laravel Datatables Export stats

Downloads
70.8K
Stars
24
Open Issues
2
Forks
15

View on GitHub →

Laravel DataTables Queued Export Plugin.

Laravel DataTables Export Plugin

This package is a plugin of Laravel DataTables for handling server-side exporting using Queue, OpenSpout and Livewire.

Requirements

Documentations

Laravel Version Compatibility

Laravel Package
8.x 0.x
9.x 1.x
10.x 10.x

Quick Installation

composer require yajra/laravel-datatables-export

The package also requires batch job:

php artisan queue:batches-table
php artisan migrate

Service Provider (Optional since Laravel 5.5+)

Yajra\DataTables\ExportServiceProvider::class

Configuration and Assets (Optional)

$ php artisan vendor:publish --tag=datatables-export --force

Usage

  1. Add the export-button livewire component on your view file that uses dataTable class.
 
<livewire:export-button :table-id="$dataTable->getTableId()"/>
  1. On your DataTable class, use WithExportQueue
use Yajra\DataTables\WithExportQueue;
 
class PermissionsDataTable extends DataTable
{
use WithExportQueue;
 
...
}
  1. Run your queue worker. Ex: php artisan queue:work

Purging exported files

On app\Console\Kernel.php, register the purge command

$schedule->command('datatables:purge-export')->weekly();

Export Filename

You can set the export filename by setting the property.

 
<livewire:export-button :table-id="$dataTable->getTableId()" filename="my-table.xlsx"/>
<livewire:export-button :table-id="$dataTable->getTableId()" filename="my-table.csv"/>
 
<livewire:export-button :table-id="$dataTable->getTableId()" :filename="$filename"/>

Export Type

You can set the export type by setting the property to csv or xlsx. Default value is xlsx.

 
<livewire:export-button :table-id="$dataTable->getTableId()" type="xlsx"/>
<livewire:export-button :table-id="$dataTable->getTableId()" type="csv"/>

Set Excel Sheet Name

Option 1: You can set the Excel sheet name by setting the property.

 
<livewire:export-button :table-id="$dataTable->getTableId()" sheet-name="Monthly Report"/>

Option 2: You can also set the Excel sheet name by overwriting the method.

protected function sheetName() : string
{
return "Yearly Report";
}

Formatting Columns

You can format the column by setting it via Column definition on you DataTable service class.

Column::make('mobile')->exportFormat('00000000000'),

The format above will treat mobile numbers as text with leading zeroes.

Numeric Fields Formatting

The package will auto-detect numeric fields and can be used with custom formats.

Column::make('total')->exportFormat('0.00'),
Column::make('count')->exportFormat('#,##0'),
Column::make('average')->exportFormat('#,##0.00'),

Date Fields Formatting

The package will auto-detect date fields when used with a valid format or is a DateTime instance.

Column::make('report_date')->exportFormat('mm/dd/yyyy'),
Column::make('created_at'),
Column::make('updated_at')->exportFormat(NumberFormat::FORMAT_DATE_DATETIME),

Valid Date Formats

Valid date formats can be adjusted on datatables-export.php config file.

'date_formats' => [
'mm/dd/yyyy',
NumberFormat::FORMAT_DATE_DATETIME,
NumberFormat::FORMAT_DATE_YYYYMMDD,
NumberFormat::FORMAT_DATE_XLSX22,
NumberFormat::FORMAT_DATE_DDMMYYYY,
NumberFormat::FORMAT_DATE_DMMINUS,
NumberFormat::FORMAT_DATE_DMYMINUS,
NumberFormat::FORMAT_DATE_DMYSLASH,
NumberFormat::FORMAT_DATE_MYMINUS,
NumberFormat::FORMAT_DATE_TIME1,
NumberFormat::FORMAT_DATE_TIME2,
NumberFormat::FORMAT_DATE_TIME3,
NumberFormat::FORMAT_DATE_TIME4,
NumberFormat::FORMAT_DATE_TIME5,
NumberFormat::FORMAT_DATE_TIME6,
NumberFormat::FORMAT_DATE_TIME7,
NumberFormat::FORMAT_DATE_XLSX14,
NumberFormat::FORMAT_DATE_XLSX15,
NumberFormat::FORMAT_DATE_XLSX16,
NumberFormat::FORMAT_DATE_XLSX17,
NumberFormat::FORMAT_DATE_YYYYMMDD2,
NumberFormat::FORMAT_DATE_YYYYMMDDSLASH,
]

Force Numeric Field As Text Format

Option to force auto-detected numeric value as text format.

Column::make('id')->exportFormat('@'),
Column::make('id')->exportFormat(NumberFormat::FORMAT_GENERAL),
Column::make('id')->exportFormat(NumberFormat::FORMAT_TEXT),

Auto Download

Option to automatically download the exported file.

 
<livewire:export-button :table-id="$dataTable->getTableId()" filename="my-table.xlsx" auto-download="true"/>

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email aqangeles@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Yajra Laravel Datatables Export Related Articles

New Colors Added in Tailwind CSS v4.2 image

New Colors Added in Tailwind CSS v4.2

Read article
Manage PostgreSQL Databases Directly in VS Code with Microsoft's Extension image

Manage PostgreSQL Databases Directly in VS Code with Microsoft's Extension

Read article
Handle Nested Data Structures in PHP with the Data Block Package image

Handle Nested Data Structures in PHP with the Data Block Package

Read article
Introducing Mailviews Early Access image

Introducing Mailviews Early Access

Read article
Infinite Scroll in Inertia v2.2 image

Infinite Scroll in Inertia v2.2

Read article
Export Large Amounts of Data into Feeds with the Feeds Package for Laravel image

Export Large Amounts of Data into Feeds with the Feeds Package for Laravel

Read article
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift