Filament Custom Action to Export Table Data to Excel

Joshua Idunnu Paul
2 min readMay 26, 2024

--

Photo by Rubaitul Azad on Unsplash

Exporting data from your Filament admin panel to an Excel file is a common requirement. This article will guide you through creating a custom action to export filtered data to excel, this means that if filters are applied to your table, the export will include only the records that match the filter criteria, we can also modify the format in which our data is exported.

Prerequisite: Before proceeding with this guide, ensure you have the maatwebsite/excel package installed in your Laravel project. If you haven't installed it yet, you can follow the setup instructions provided in the official documentation.

In this example, we will be using an OrderResource, in your own case, it can be any model resource.

<?php

use Filament\Tables\Actions\Action;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\OrdersExport;
use Filament\Resources\Resource;

class OrderResource extends Resource
{
protected function getHeaderActions(): array
{
return [
Action::make('Export')
->action(fn () => Excel::download(new OrdersExport(
$this->table->paginated(false)->getRecords()
), 'orders.csv'))
->icon('heroicon-o-arrow-down-on-square'),
];
}
}

In the code above:

  • Action::make('Export') creates a new action named "Export".
  • ->action(fn () => Excel::download(...)) defines the action's behavior, which is to download an Excel file using the OrdersExport class.
  • new OrdersExport($this->table->paginated(false)->getRecords()) creates a new instance of the export class with the current table records, The paginated(false) ensures that it retrieves all records matching the current filters, not just those on the current page, thereby considering any filters applied to the table.
  • ->icon('heroicon-o-arrow-down-on-square') sets an icon for the action button.
<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use App\Models\Order;
use Illuminate\Support\Collection;

class OrdersExport implements FromCollection, WithHeadings
{
protected Collection $orders;

public function __construct(Collection $orders)
{
$this->orders = $orders;
}

/**
* @return \Illuminate\Support\Collection
*/
public function collection()
{
return $this->orders->map(function($order) {
return [
'Order ID' => (int) $order->id,
'Customer Name' => (string) $order->customer_name,
'Order Date' => (string) $order->created_at->format('Y-m-d H:i:s'),
'Total Amount' => (float) $order->total_amount,
'Status' => (string) $order->status,
];
});
}

/**
* @return array
*/
public function headings(): array
{
return [
'Order ID',
'Customer Name',
'Order Date',
'Total Amount',
'Status',
];
}
}

In the code above:

  • Implements WithHeadings: Adds headers to the exported data.
  • Data Formatting: The map function in the collection method formats each order's data:
  • Order ID: Casts to integer.
  • Customer Name: Casts to string.
  • Order Date: Formats the date.
  • Total Amount: Casts to float.
  • Status: Casts to string.
  • Headings Method: Defines the headers for the exported data.

With this approach, you can create powerful exports in your Filament admin panel that respect applied filters and ensure the exported data is well-formatted and type-casted. This makes the data more usable and provides a better user experience.

--

--