Blog Detail

30

Nov
Easily Configure Excel Exports in Filament via page action cover image

arrow_back Easily Configure Excel Exports in Filament via page action

Filament Excel is an awesome package that helps you to easily configure your Excel exports in Filament via a bulk or page action.

Installation

You can install this package via composer. This will download the package and Laravel Excel. This package
requires PHP 8.0 and Filament 2.0

composer require pxlrbt/filament-excel

For Laravel 9

composer require psr/simple-cache:^2.0 pxlrbt/filament-excel

Quickstart

Starting with v0.2 Filament Excel should work with both filament/filament and filament/tables packages. The most simple usage is just adding ExportBulkAction to your bulk actions.

Example for admin package

<?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()
            ]);
    }
}

Example for table package

<?php

namespace App\Filament\Resources;

use pxlrbt\FilamentExcel\Actions\Tables\ExportBulkAction;

public function getTableBulkActions()
{
    return  [
        ExportBulkAction::make()
    ];
}

Usage

Filament Excel comes with three actions you can use:

  • Actions\Tables\ExportBulkAction for table bulk actions
  • Actions\Tables\ExportAction for table header actions
  • Actions\Pages\ExportAction for record pages

Without further configuration, they will try to resolve the fields from the table or form definition and output an Excel file.

Multiple export classes

You can overwrite the default export class and also configure multiple exports with different settings. The user will be shown a modal to select the export class he wants to use.

use pxlrbt\FilamentExcel\Actions\Tables\ExportAction;
use pxlrbt\FilamentExcel\Exports\ExcelExport;

ExportAction::make()->exports([
    ExcelExport::make('table')->fromTable(),
    ExcelExport::make('form')->fromForm(),
])

Closure customization

Many of the functions for customizing the export class, accept a Closure that gets passed dynamic data:

use pxlrbt\FilamentExcel\Actions\Tables\ExportAction;
use pxlrbt\FilamentExcel\Exports\ExcelExport;

ExportAction::make()->exports([
    ExcelExport::make('table')->withFilename(fn ($resource) => $resource::getLabel()),
])

Filename

The filename is set via ->withFilename():

use pxlrbt\FilamentExcel\Actions\Tables\ExportAction;
use pxlrbt\FilamentExcel\Exports\ExcelExport;

ExportAction::make()->exports([
    // Pass a string
    ExcelExport::make()->withFilename(date('Y-m-d') . ' - export'),
    
    // Or pass a Closure
    ExcelExport::make()->withFilename(fn ($resource) => $resource::getLabel())
])

Export types

You can set the file type via ->withWriterType():

use pxlrbt\FilamentExcel\Actions\Tables\ExportAction;
use pxlrbt\FilamentExcel\Exports\ExcelExport;

ExportAction::make()->exports([
    ExcelExport::make()->withWriterType(\Maatwebsite\Excel\Excel::XLSX),
])

This package has a lot of features and options wit code examples, For more details, please visit Github.

Published at : 30-11-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project