Filament Excel

repository·main·Indexed 19 days ago

https://github.com/pxlrbt/filament-excel

A package for Filament that allows developers to configure and trigger Excel and CSV exports within Filament resources via bulk or page actions. It provides specialized action classes like ExportBulkAction, ExportAction, and the ExcelExport class for managing columns, filenames, writer types, and queued exports for large datasets. Compatible with Filament v2, v3, and v4 depending on the plugin version.

Tokens
7K
Snippets
31
Records
33
Agent score
66%

What's inside pxlrbt/filament-excel

  1. Customize exports using Closures

    main

    Many customization methods on the ExcelExport class accept a Closure. This allows you to inject dynamic data into your export configuration. The following arguments are available within these Closures:

    • $livewire: The Livewire component instance (not available for queued exports).
    • $livewireClass: The class name of the Livewire component.
    • $resource: The Resource class.
    • $model: The Model class.
    • $recordIds: The IDs of the selected records (used in Bulk Actions).
    • $query: The Eloquent query builder instance.
    use pxlrbt\FilamentExcel\Actions\Tables\ExportAction;
    use pxlrbt\FilamentExcel\Exports\ExcelExport;
    
    ExportAction::make()->exports([
        ExcelExport::make('table')->withFilename(fn ($resource) => $resource::getLabel()),
    ])
  2. Create custom exports by extending ExcelExport

    main

    To separate export logic from your resources or to achieve advanced customization, extend the ExcelExport class and use the setUp() method to configure columns and filenames.

    use pxlrbtilament-excel\Actions\Tables\ExportAction;
    use pxlrbtilament-excel\Exports\ExcelExport;
    use pxlrbtilament-excel\Columns\Column;
    
    class CustomExport extends ExcelExport
    {
        public function setUp()
        {
            $this->withFilename('custom_export');
            $this->withColumns([
                Column::make('name'),
                Column::make('email'),
            ]);
        }
    }
  3. Apply custom styles to exports

    main

    To apply advanced styling (like bold headers or specific cell colors), create a custom export class that extends ExcelExport and implements the Maatwebsite\Excel\Concerns\WithStyles interface. This allows you to use the styles() method to return an array of styles mapped to cell ranges.

    use pxlrbt\FilamentExcel\Exports\ExcelExport;
    use pxlrbt\FilamentExcel\Columns\Column;
    use Maatwebsite\Excel\Concerns\WithStyles;
    use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
    
    class CustomExport extends ExcelExport implements WithStyles
    {
        public function setUp()
        {
            $this->withFilename('custom_export');
            $this->withColumns([
                Column::make('name'),
                Column::make('email'),
            ]);
        }
    
        public function styles(Worksheet $sheet)
        {
            return [
                1    => ['font' => ['bold' => true]], // Header row
                'B2' => ['font' => ['italic' => true]],
                'C'  => ['font' => ['size' => 16]],
            ];
        }
    }
  4. Install Filament Excel via Composer

    main

    Install the package using Composer. This will also install laravel-excel as a dependency.

    Plugin VersionFilament VersionPHP Version
    1.x2.x> 8.0
    2.x3.x> 8.1
    3.x4.x, 5.x> 8.1
    composer require pxlrbt/filament-excel
  5. Use queued exports for large datasets

    main

    For exports involving many records, use ->queue() to process the export in a background job. This prevents timeout issues and improves user experience.

    • Chunking: Use ->withChunkSize(int) to adjust the number of records processed per job.
    • Queue Configuration: You can specify a custom queue name and connection via ->queue(queue: 'name', connection: 'name').
    • Lifecycle: Temporary files are deleted after the first download. Un-downloaded files are deleted by a scheduled command after 24 hours.
    use pxlrbt\FilamentExcel\Actions\Tables\ExportAction;
    use pxlrbt\FilamentExcel\Exports\ExcelExport;
    
    // Basic queued export
    ExcelExport::make()->queue()
    
    // Queued export with custom chunk size
    ExcelExport::make()->queue()->withChunkSize(100)
    
    // Queued export with custom queue and connection
    ExcelExport::make()->queue(queue: 'exports', connection: 'redis')
  6. Use Filament Excel Actions

    main

    Filament Excel provides three primary action classes to trigger exports within your Filament application:

    • Actions\Tables\ExportBulkAction: For bulk actions on table rows.
    • Actions\Tables\ExportAction: For header actions on a table.
    • Actions\Pages\ExportAction: For record pages.

    By default, these actions attempt to resolve fields from your existing table or form definitions to generate the Excel file.

    use pxlrbt\FilamentExcel\Actions\Tables\ExportAction;
    
    // In a Filament Table
    ExportAction::make()
  7. Upgrade to Filament v4

    main

    When upgrading to Filament v4, you must use version 3.x of the plugin. Note that Action classes have been simplified into ExportBulkAction (for tables) and ExportAction (for pages).

    composer require pxlrbt/filament-excel:3.0
  8. How ExcelExport resolves queries and models

    main

    The ExcelExport class is designed to automatically detect the context of the export based on the provided Livewire component (e.g., a Filament Table or Relation Manager):

    1. Model Detection: It attempts to find the model class via the Livewire component's table relationship, the associated Filament Resource, or the record currently being viewed.
    2. Query Resolution:
      • If useTableQuery is enabled, it uses the filtered and sorted query from the Livewire table.
      • Otherwise, it starts with a base query from the detected model class.
      • If specific recordIds are provided (via hydrate()), the query is automatically constrained to those IDs.
    3. Query Modification: You can provide a closure via modifyQueryUsing to further customize the query before execution.
  9. Fix dependency issues on Laravel 9+

    main

    If composer require fails on Laravel 9 or greater due to the simple-cache dependency, you must explicitly specify psr/simple-cache version ^2.0 to satisfy the PhpSpreadsheet dependency.

    composer require psr/simple-cache:^2.0 pxlrbt/filament-excel
  10. Quickstart: Add ExportBulkAction to a Filament Table

    main

    The simplest way to use Filament Excel is to add the ExportBulkAction to your table's bulk actions. This works with both filament/filament and filament/tables packages.

    <?php
    
    namespace App\Filament\Resources;
    
    use pxlrbt\FilamentExcel\Actions\Tables\ExportBulkAction;
    
    class UserResource extends Resource
    {
        public static function table(Table $table): Table
        {
            return $table
                ->columns([
                    //   
                ])
                ->bulkActions([
                    ExportBulkAction::make()
                ]);
        }
    }
  11. Quickstart: Add ExportBulkAction to a separate table package

    main

    If you are using a separate table package, you can return the ExportBulkAction within your bulk actions method.

    <?php
    
    namespace App\Filament\Resources;
    
    use pxlrbt\FilamentExcel\Actions\Tables\ExportBulkAction;
    
    public function getTableBulkActions()
    {
        return  [
            ExportBulkAction::make()
        ];
    }
  12. Configure multiple export classes

    main

    You can overwrite the default export behavior or provide multiple export options. When multiple exports are configured, Filament will present the user with a modal to select which export class they wish to use.

    use pxlrbt\FilamentExcel\Actions\Tables\ExportAction;
    use pxlrbt\FilamentExcel\Exports\ExcelExport;
    
    ExportAction::make()->exports([
        ExcelExport::make('table')->fromTable(),
        ExcelExport::make('form')->fromForm(),
    ])