• Stars
    star
    330
  • Rank 127,657 (Top 3 %)
  • Language
    PHP
  • License
    MIT License
  • Created almost 3 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Excel Export for Filament Admin Resources

header

Filament Excel

Latest Version on Packagist Software License GitHub Workflow Status Total Downloads

Easily configure your Excel exports in Filament via a bulk or page action.

demo.mp4

Installation

Install via Composer. This will download the package and Laravel Excel.

Requires PHP 8.0 and Filament 2.0

composer require pxlrbt/filament-excel

Laravel > 9

If composer require fails on Laravel 9 or greater because of the simple-cache dependency, you will have to specify the psr/simple-cache version as ^2.0 in your composer.json to satisfy the PhpSpreadsheet dependency. You can install both at the same time as:

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 customising 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()),
])

The following arguments are available:

  • $livewire: Livewire component (not available for queued exports)
  • $livewireClass: Livewire component class
  • $resource: Resource class
  • $model: Model class
  • $recordIds: IDs of selected records (Bulk Action)
  • $query: The builder instance

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),
])

Defining columns

When using ->fromForm()/->fromTable()/->fromModel() the columns are resolved from your table or form definition. You can also provide columns manually, append columns or overwrite generated columns.

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

ExportAction::make()->exports([
    ExcelExport::make()->withColumns([
        Column::make('name'),
        Column::make('created_at'),
        Column::make('deleted_at'),
    ]),
])

You can also include only a subset of columns (->only()) or exclude certain ones (->except()):

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

ExportAction::make()->exports([
    ExcelExport::make()->fromTable()->except([
        'created_at', 'updated_at', 'deleted_at',
    ]),
    
    ExcelExport::make()->fromTable()->only([
        'id', 'name', 'title',
    ]),
])

When you neither pass ->only() nor ->except() the export will also respect the $hidden attributes of your model, for example the password on the user model. You can disable this by passing an empty array ->except([]).

Headings

When using ->fromForm()/->fromTable()/->fromModel() the headings are resolved from your table or form definition. You can also overwrite headings:

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

ExportAction::make()->exports([
    ExcelExport::make()->withColumns([
        Column::make('name')->heading('User name'),
        Column::make('email')->heading('Email address'),
        Column::make('created_at')->heading('Creation date'),
    ]),
])

If you want to use the column names and don't like the headings auto generated you can use ->withNamesAsHeadings(). To disable headings entirely you can append ->withoutHeadings()

Formatting

Every column can be formatted by providing a Closure. Additional to the default parameters you get access to $state and $record.

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

ExportAction::make()->exports([
    ExcelExport::make()->withColumns([
        Column::make('email')
            ->formatStateUsing(fn ($state) => str_replace('@', '[at]', $state)),
            
        Column::make('name')
            ->formatStateUsing(fn ($record) => $record->locations->pluck('name')->join(','),
    ]),
])

Columns are auto-scaled to fit the content. If you want to overwrite this with a custom column width you can do so:

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

ExportAction::make()->exports([
    ExcelExport::make()->withColumns([
        Column::make('email')->width(10)
    ]),
])

The underlying package PhpSpreadsheet provides various options for Excel column formatting. Inspect the NumberFormat list for the full list.

use pxlrbt\FilamentExcel\Actions\Tables\ExportAction;
use pxlrbt\FilamentExcel\Exports\ExcelExport;
use pxlrbt\FilamentExcel\Columns\Column;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

ExportAction::make()->exports([
    ExcelExport::make()->withColumns([
        Column::make('currecy')->format(NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE)
    ]),
])

User input

You can let the user pick a filename and writer type by using ->askForFilename() and ->askForWriterType():

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

ExportAction::make()->exports([
    ExcelExport::make()
        ->askForFilename()
        ->askForWriterType()
])

You can also use the users input inside a Closure:

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

ExportAction::make()->exports([
    ExcelExport::make()
        ->askForFilename()
        ->withFilename(fn ($filename) => 'prefix-' . $filename)
])

Modify the query

You can modify the query that is used to retrieve the model by using ->modifyQueryUsing():

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

ExportAction::make()->exports([
    ExcelExport::make()
        ->fromTable()
        ->modifyQueryUsing(fn ($query) => $query->where('exportable', true))
])

Queued exports

Exports for resources with many entries can take some time and therefore can be queued with ->queue(). They will be processed in background jobs and the user will be notified with a notification on the next page load (or when Livewire is polling).

The temporary file will be deleted after the first download. Files that are not downloaded will be deleted by a scheduled command after 24 hours.

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

ExportAction::make()->exports([
    ExcelExport::make()->queue()
])

The size of exported records per Job can be adjusted by using ->withChunkSize():

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

ExportAction::make()->exports([
    ExcelExport::make()->queue()->withChunkSize(100)
])

Custom exports

If you need even more customization or want to clean up your resources by separating the export code, you can extend the ExcelExport class and configure it using setUp():

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

class CustomExport extends ExcelExport
{
    
    public function setUp()
    {
        $this->withFilename('custom_export');
        $this->withColumns([
            Column::make('name'),
            Column::make('email'),
        ]);
    }
}

Contributing

If you want to contribute to this packages, you may want to test it in a real Filament project:

  • Fork this repository to your GitHub account.
  • Create a Filament app locally.
  • Clone your fork in your Filament app's root directory.
  • In the /filament-excel directory, create a branch for your fix, e.g. fix/error-message.

Install the packages in your app's composer.json:

"require": {
    "pxlrbt/filament-excel": "dev-fix/error-message as main-dev",
},
"repositories": [
    {
        "type": "path",
        "url": "filament-excel"
    }
]

Now, run composer update.

Credits

This package is based on the excellent Laravel Nova Excel Package by SpartnerNL and ported to Filament.

More Repositories

1

move-wsl

Easily move your WSL distros VHDX file to a new location.
PowerShell
1,411
star
2

filament-spotlight

Quickly navigate your Filament pages
PHP
223
star
3

filament-activity-log

Spatie activity log integration into Filament
PHP
134
star
4

filament-environment-indicator

Never confuse your tabs with different Filament environments again.
PHP
110
star
5

laravel-pdfable

Keep your PDFs logic in one place.
PHP
38
star
6

php-scoper-prefix-remover

PHP
10
star
7

cobalt2-jetbrains

Cobalt2 Theme for Jetbrains IDE.
Sass
9
star
8

markdown-css

Modern CSS style for nicely formatted markdown exports.
CSS
9
star
9

third-party-checker

Quickly check your websites for third party requests.
JavaScript
9
star
10

acf-configurator

A wrapper for easy local Advanced Custom Fields configuration in php.
PHP
8
star
11

wordpress-notifier

WordPress Notifier is an object oriented helper library for handling WordPress admin notices inside a plugin or theme. It supports persistent notices and dismissible notices out of the box and is extendable.
PHP
4
star
12

lightroom-filematcher

PHP Scripts that matches LR meta data to local files and renames them
PHP
3
star
13

wordpress-cf7-cleverreach-integration

WordPress Plugin: Integrates CleverReachยฎ into Contact Form 7
PHP
2
star
14

filament-translate-action

PHP
1
star
15

website-diff

Utility tool that bundles a crawler and BackstopJS for visual regression testing.
JavaScript
1
star
16

wordpress-cf7-weclapp-integration

Wordpress Plugin: Integrates WeClapp into Contact Form 7 forms
PHP
1
star
17

faker-better-unique

Replaces the default Faker with an extended Faker instance that adds `betterUnique()` โ€“ `unique()` based on a key.
PHP
1
star