Skip to content

Exporting ​

Inertia Table may export the current view of any table to CSV, Excel, or PDF through Laravel Excel. Pick the columns and formats you want, run the export inline for small datasets, or push large exports onto the queue and notify users when the file is ready.

Introduction ​

Before continuing, make sure the Laravel Excel package is installed:

bash
composer require maatwebsite/excel

Defining Exports ​

The Table class exposes an exports() method for defining export options. Create an Export instance via the static make() method and return it from exports().

php
use InertiaUI\Table\Table;

class Users extends Table
{
    public function exports(): array
    {
        return [
            Export::make(),
        ];
    }
}

By default, the export type is Excel and the label is Excel Export. The make() method accepts five arguments to customize the export:

php
use Maatwebsite\Excel\Excel;

Export::make(
    label: 'CSV Export',
    filename: 'users.csv',
    type: Excel::CSV,
    authorize: true,
    events: []
);

The type must be one of the constants from the Maatwebsite\Excel\Excel class. Some types, like PDF, require additional configuration.

Filtering and Selected Rows ​

By default, the export includes all rows, even when filters are applied or rows are selected in the frontend. You may change this behavior via the limitToFilteredRows or limitToSelectedRows options on make().

php
Export::make(
    limitToFilteredRows: true,
    limitToSelectedRows: true,
);

Alternatively, you may call the limitToFilteredRows() or limitToSelectedRows() methods:

php
Export::make()
    ->limitToFilteredRows()
    ->limitToSelectedRows();

To set this as the default for all tables, call the static defaultLimitToFilteredRows() and defaultLimitToSelectedRows() methods on the Export class, typically in your AppServiceProvider:

php
use InertiaUI\Table\Export;

Export::defaultLimitToFilteredRows();
Export::defaultLimitToSelectedRows();

Authorization ​

Authorization works the same way as with Row Actions. The authorize option can be a boolean or a Closure.

Export Events ​

The events array lets you hook into the underlying Laravel Excel events:

php
use Maatwebsite\Excel\Events\BeforeExport;

Export::make(events: [
    BeforeExport::class => function (BeforeExport $event) {
        $event->writer->getProperties()->setCreator('John Doe');
    },
]);

See the Laravel Excel documentation for the full list of events.

Customizing Exports ​

Each column may be customized at export time.

Column Mapping ​

To change the content of a column when exporting, pass a Closure to the exportAs argument or call the exportAs() method:

php
TextColumn::make('reference', exportAs: function (string $reference) {
    return "#REF-{$reference}";
});

The closure accepts a second argument, which is the Model instance.

php
TextColumn::make('name')->exportAs(function (string $name, User $user) {
    return $user->is_active ? $name : "Inactive: {$name}";
});

Column Formatting ​

You may choose a custom format for each column by passing a Closure or string to the exportFormat argument, or by calling the exportFormat() method:

php
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

TextColumn::make('amount', exportFormat: NumberFormat::FORMAT_CURRENCY_USD_SIMPLE);

TextColumn::make('amount')->exportFormat(NumberFormat::FORMAT_CURRENCY_USD_SIMPLE);

Column Styling ​

You may customize the column styling by passing a Closure or array to the exportStyle argument, or by calling the exportStyle() method:

php
use PhpOffice\PhpSpreadsheet\Style\Style;

TextColumn::make('name', exportStyle: ['font' => ['bold' => true]]);

TextColumn::make('email')->exportStyle(function (Style $style) {
    $style->getFont()->setBold(true);
});

Exclude Columns ​

To exclude a column from being exported, pass false to the exportAs argument, or call the dontExport() method.

php
TextColumn::make('Name', exportAs: false);

TextColumn::make('Name')->dontExport();

Defining a Custom Export ​

For full control over the export, you may supply your own logic via the using argument or the using() method:

php
use App\Tables\Users;
use InertiaUI\Table\Export;
use InertiaUI\Table\Http\ExportRequest;
use Illuminate\Contracts\Database\Eloquent\Builder;

Export::make()->using(function (Users $table, Export $export, ExportRequest $request, Builder $query) {
    // return response()->download(...);
});

The limitToFilteredRows and limitToSelectedRows options are already applied to the given Query Builder ($query). You may also pass an invokable class:

php
Export::make(using: new CustomUserExport);

Custom Export with Redirect Response ​

The using() callback expects a downloadable response. To redirect the user to a different page instead, call the asDownload() method with false:

php
Export::make()
    ->asDownload(false)
    ->using(function () {
        return to_route('dashboard');
    });

Alternatively, you may use the redirectBackWithDialog() method to stay on the same page and show a dialog:

php
Export::make()->redirectBackWithDialog();

// With custom title and message
Export::make()->redirectBackWithDialog(
    title: 'Exporting your data',
    message: 'The export is being processed.'
);

Queued Exports ​

For large datasets, you may queue the export and notify the user when it is ready by calling the queue() method:

php
Export::make()->queue(
    filename: 'data-export.xlsx',
    disk: 's3'
);

Instead of downloading the file, the user sees a dialog indicating the export is being processed. You may customize the dialog's title and message via the title and message options:

php
Export::make()->queue(
    filename: 'data-export.xlsx',
    disk: 's3',
    title: 'Exporting your 2024 data',
    message: 'The export is being processed.'
);

You may also notify the user when the export is ready, for example by sending an email. Interact with the queued job by passing a Closure to the withQueuedJob argument or by calling the withQueuedJob() method:

php
Export::make()->queue()->withQueuedJob(function ($job) {
    $job->chain([
        new SendExportNotification(auth()->user()),
    ]);
});

Likewise, you may set the queue or delay for the job:

php
Export::make()->queue(
    withQueuedJob: fn ($job) => $job->onQueue('exports')->delay(now()->addMinutes(5))
);

Default Job Configuration ​

Instead of passing a disk and queue name to each export, you may use the static defaultQueueName() and defaultQueueDisk() methods on the Export class, typically in your AppServiceProvider:

php
use InertiaUI\Table\Export;

Export::defaultQueueName('exports');
Export::defaultQueueDisk('s3');

Custom Redirect on Queued Export ​

To skip the dialog and redirect the user to a different page, pass a URL to the redirect option. It accepts a string, a RedirectResponse, or a Closure. A redirectToRoute helper is also available:

php
Export::make()
    ->queue()
    ->redirect('/exports')
    ->redirect(to_route('exports.index'))
    ->redirect(fn () => route('exports.index'))
    ->redirectToRoute('exports.index');

Data Attributes ​

You may add Data Attributes to the Export button via the dataAttributes argument on make() or the dataAttributes() method. Keys do not need to be prefixed with data-.

php
Export::make()->dataAttributes([
    'contents' => 'all-users',
]);

Targeting Exports With CSS ​

You may target the Export button by its data attributes. For example, to style an Export with the data-content="all-users" attribute:

css
a[data-content="all-users"] {
    /* ... */
}

Metadata ​

You may attach metadata to an Export via the meta argument on make() or the meta() method:

php
Export::make()->meta([
    'key' => 'value',
]);

The default Table component ignores this metadata and does not pass it to any DOM element. It becomes useful when extending the Table component using slots, or when building a custom Table component.