Filament Exports – Transform Your Data with CSV and Excel!

Filament Exports – Transform Your Data with JSON and Excel!

In this article, we are going to discuss about how we can export the data/records from filament admin panel in excel or csv format.

Before starting, make sure you have published migration for jobs and notification as Filament export uses those features. Furthermore, don’t forget to check it out on documentation.

Install dependencies

To use this feature, we need to add the following migrations:

php artisan vendor:publish --tag=filament-actions-migrations

After that hit:

php artisan migrate

Create a Exporter Class

Before, creating an export action, we need to have a exporter class which will handle the export mechanism. So, to create that class just hit the following command in your terminal.

php artisan make:filament-exporter Product

So, the above command will generate us the ProductExporter class which boilerplate is below:

<?php

namespace App\Filament\Exports;

use App\Models\Product;
use Filament\Actions\Exports\ExportColumn;
use Filament\Actions\Exports\Exporter;
use Filament\Actions\Exports\Models\Export;

class ProductExporter extends Exporter
{
    protected static ?string $model = Product::class;

    public static function getColumns(): array
    {
        return [
            //
        ];
    }

    public static function getCompletedNotificationBody(Export $export): string
    {
        $body = 'Your product export has completed and ' . number_format($export->successful_rows) . ' ' . str('row')->plural($export->successful_rows) . ' exported.';

        if ($failedRowsCount = $export->getFailedRowsCount()) {
            $body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to export.';
        }

        return $body;
    }
}

Define Export Columns

After that, we need to add the columns inside the getColumns() method. Let’s say we want to export the following columns:

  • Name
  • Quantity
  • Price
  • Safety Stock

So, for that lets add each columns:

 public static function getColumns(): array
    {
        return [
            ExportColumn::make('name'),
            ExportColumn::make('quantity'),
            ExportColumn::make('price'),
            ExportColumn::make('safety_stock'),
        ];
    }

Create Export Action

Now, we need to add an export action . Since we are exporting products, Lets go to the ListProduct page and add the following action:

protected function getHeaderActions(): array
    {
        return [
            Actions\CreateAction::make(),
            ExportAction::make()
                ->exporter(ProductExporter::class)
        ];
    }

Conclusion

Now, after we add the above action, we can easily use the export action feature. Make sure, you are running job and database notifications.

If you have any confusion about this post, you can watch us on youtube or comment us below.

1 thought on “Filament Exports – Transform Your Data with CSV and Excel!”

  1. Pingback: Boost Your Workflow: Mastering Filament Import Features in Laravel - Devnestle

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top