F
Filament10mo ago
Noor

Filtered Data

I want to export only filtered table data to a csv , For instance, 10 records are in the database, and after using the filter only 3 reflect in the table, now I want these 3 records to be exported and generate a Csv. or is there any way with bulk actions by selecting them?
3 Replies
DrByte
DrByte10mo ago
You can each() over the list. See the delete example in the docs at: https://filamentphp.com/docs/3.x/tables/actions#bulk-actions
Noor
Noor10mo ago
Tables\Actions\BulkAction::make('export') ->action(function (Collection $records) { $records = Prospect::where('interested', '=', 1)->get(); // Export the selected records to a CSV file $records->each(function (Prospect $record) { $record->export(); dd($record); }); }), like this @DrByte
DrByte
DrByte10mo ago
Actually, don't need to call each() Here's an exporter I use with the Laravel-Excel package installed: Bulk Action Button:
Tables\Actions\BulkAction::make('export-selected')
->action(fn (Collection $records) => (new MembersExport)
->forRecords($records)
->download('members-export-' . date('Y-m-d-i-h-s') . '.xlsx'))
Tables\Actions\BulkAction::make('export-selected')
->action(fn (Collection $records) => (new MembersExport)
->forRecords($records)
->download('members-export-' . date('Y-m-d-i-h-s') . '.xlsx'))
List page header Action Button, to export everything:
Actions\Action::make('export')
->action(fn () => (new MembersExport)->download('members-export-' . date('Y-m-d-i-h-s') . '.xlsx'))
Actions\Action::make('export')
->action(fn () => (new MembersExport)->download('members-export-' . date('Y-m-d-i-h-s') . '.xlsx'))
(I've also added label() and visible() and icon() etc, but you can do what you like) And the actual Exporter:
use App\Models\Member;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Query\Builder;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;

class MembersExport implements FromQuery
{
use Exportable;

protected ?Collection $records = null;

public function query(): Builder|EloquentBuilder|Relation
{
return Member::query()
->when(filled($this->records), fn (EloquentBuilder $query) => $query->whereIn('id', $this->records->pluck('id')))
->orderBy('email');
}

/*
* Used in conjunction with when() query clause to process just the tagged bulk-export selections
*/
public function forRecords(Collection $records): self
{
$this->records = $records;

return $this;
}
}
use App\Models\Member;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Query\Builder;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;

class MembersExport implements FromQuery
{
use Exportable;

protected ?Collection $records = null;

public function query(): Builder|EloquentBuilder|Relation
{
return Member::query()
->when(filled($this->records), fn (EloquentBuilder $query) => $query->whereIn('id', $this->records->pluck('id')))
->orderBy('email');
}

/*
* Used in conjunction with when() query clause to process just the tagged bulk-export selections
*/
public function forRecords(Collection $records): self
{
$this->records = $records;

return $this;
}
}
Want results from more Discord servers?
Add your server
More Posts