Skip to content

Exporting

Introduction

Tables can be exported to different formats such as CSV, Excel, and PDF. Before continuing, make sure you have installed the Laravel Excel package. You can install it via composer:

bash
composer require maatwebsite/excel

Defining Exports

The Table class has an exports() method that can be used to define all the export options. Simply create an Export instance with the static make() method and return it from the exports() method.

php
use InertiaUI\Table\Table;

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

By default, the export is set to the Excel type and has a label of 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. Note that some types, like PDF, require additional configuration.

Filtering and Selected Rows

By default, the export will include all rows, even if you have applied filters or selected rows in the frontend. You can change this behavior by passing the limitToFilteredRows or limitToSelectedRows options to the make() method.

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

Alternatively, you can call the limitToFilteredRows() or limitToSelectedRows() methods.

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

If you want this to be the default for all tables, you may call the static defaultLimitToFilteredRows() and defaultLimitToSelectedRows() methods on the Table class, for example, 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 enables you to hook into the underlying package events.

php
use Maatwebsite\Excel\Events\BeforeExport;

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

Check out the documentation for more information.

Customizing Exports

You can customize how each column is exported.

Column Mapping

If you want to change the content of a column when exporting, you can pass a Closure to the exportUsing argument. Alternatively, you can call the exportUsing() 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. Alternatively, you can call 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 can customize the column styling by passing a Closure or array to the exportStyle argument. Alternatively, you can call 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();

Define a Custom Export

If you need more control over the export, you can write your own logic to export the data. You can do this with 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(...);
});

If the export uses the limitToFilteredRows or limitToSelectedRows options, those will already be applied to the given Query Builder ($query). You may also pass an invokable class:

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

Data Attributes

You can add Data Attributes to the Export button by using the dataAttributes argument in the make() method or by using the dataAttributes method. You don't have to prefix the keys with data-.

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

Targetting Exports with CSS

You can target the Export button with specific data attributes using CSS. For example, to style the Export with the data-content="all-users" attribute, you can use the following CSS:

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

Metadata

You can add metadata to Exports by using the meta argument in the make() or by using the meta() method.

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

In the default Table component, this metadata is unused and not passed to any DOM element. However, this feature can be useful when extending the Table component using slots, or when you build a custom Table component.