Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

phil_davis's avatar

Bulk export not restricting to selected rows in table

I have a Filament table that has a bulk export action. There are 100+ rows in the table, and when I select just 2 of them and click export I see the notification that says something like "2 rows will be processed in the background." But when I open the csv file it includes every row.

Whoever wrote the table has it doing a somewhat complicated query to get the data:

return $table
            ->query(function () {
                return Member::query()
                             ->join('forms as f', function ($join) {
                                 $join->on('f.id', '=', 'm_form_module.form_id')
                                      ->where('f.slug', 'membership-information');
                             })
                             ->leftJoin('users as u', 'u.id', '=', 'm_form_module.user_id')
                             ->join('z_membership-information as mi', function ($join) {
                                 $join->on('u.id', '=', 'm_form_module.user_id')
                                      ->on('mi.submission_id', '=', 'm_form_module.submission_id');
                             })
                             ->join('contacts as c', 'u.contact_id', '=', 'c.id')
                             ->join('reporting_periods as rp', 'm_form_module.rp_id', '=', 'rp.id')
                             ->select([
                                 'm_form_module.*',
                                 'c.*',
                                 'mi.*',
                                 'rp.name as rp_name',
                                 'rp.year',
                                 'm_form_module_id as id',
                             ]);
            })

Here is where I added the export action:

->bulkActions([
                Tables\Actions\ExportBulkAction::make()
                    ->exporter(MemberExporter::class)
                    ->label('Export')
                    ->modifyQueryUsing(function (Builder $query, $records) {
                        return $query->whereIn('m_form_module_id', $records->toArray());
                    }),
            ]);

As you can see I tried to use modifyQueryUsing to manually restrict it to the selected rows. That obviously didn't work.

What's strange is that when I add a breakpoint in the __invoke method of vendor/filament/actions/src/Exports/Exporter.php, I made a watcher for $record->id and the invoke method is hit once for each selected row, and the $record->id value correctly matches the rows selected. This is what I would expect if it were working correctly.

I guess it's also worth noting that MemberExporter extends BaseExporter, which extends the default Filament exporter class:

use Filament\Actions\Exports\Exporter as FilamentExporter;
use Illuminate\Contracts\Auth\Authenticatable;

abstract class BaseExporter extends FilamentExporter
{
    public function __construct(Export $export, array $columnMap, array $options)
    {
        parent::__construct($export, $columnMap, $options);
    }

    public function getJobMiddleware(): array
    {
        app()->bind(Authenticatable::class, User::class); // Not sure why needed

        return parent::getJobMiddleware();
    }

    public static function getCompletedNotificationBody(Export|\Filament\Actions\Exports\Models\Export $export): string
    {
        $body = 'Your member 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;
    }
}

Though I don't really see much there that I would suspect has anything to do with this issue.

Has anyone got any ideas? I saw that the exporter class has a modifyQuery method and I thought maybe I could try manually setting the selected rows there like I tried on the export action, but I can't figure out how to access which rows are selected.

0 likes
0 replies

Please or to participate in this conversation.