fiament 导出excel,怎么在浏览器中导出而不是在本地laravel代码storage/app/public导出
1. 运行环境
mac m2
PHP:8.3.11
laravel:10.48.20
filament:3.2
2. 问题描述?
根据官方文档步骤导出excel
Export action - Actions - Filament (laravel-filament.cn)
php artisan queue:batches-table
php artisan notifications:table
php artisan vendor:publish --tag=filament-actions-migrations
php artisan migrate
php artisan make:filament-exporter Product --generate
这是资源控制器代码
如果报错:
Method Filament\Actions\ExportAction::table does not exist.
在导出excel的时候要这样写
\Filament\Tables\Actions\ExportAction::make()
参考链接:github.com/filamentphp/filament/di...
<?php
namespace App\Filament\Resources;
use App\Filament\Resources\PatientResource\Pages;
use App\Filament\Resources\PatientResource\RelationManagers;
use App\Models\Patient;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Filament\Forms\Components\TextInput;
use Filament\Tables\Actions\CreateAction;
use App\Filament\Exports\PatientExporter;
class PatientResource extends Resource
{
protected static ?string $model = Patient::class;
protected static ?string $navigationLabel = '病人管理';
// 设置列表页面的标题
protected static ?string $modelLabel = '病人管理';
// 设置列表页面中单个条目的名称
// protected static ?string $modelSingularLabel = '文章';
// 设置列表页面中多个条目的名称
// protected static ?string $modelPluralLabel = '文章管理';
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),
Forms\Components\Select::make('type')
->options([
'cat' => 'Cat',
'dog' => 'Dog',
'rabbit' => 'Rabbit',
])
->required(),
Forms\Components\DatePicker::make('date_of_birth')
->required()
->maxDate(now()),
Forms\Components\Select::make('owner_id')
->relationship('owner', 'name')
->searchable()
->preload()
->createOptionForm([
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),
Forms\Components\TextInput::make('email')
->label('Email address')
->email()
->required()
->maxLength(255),
Forms\Components\TextInput::make('phone')
->label('Phone number')
->tel()
->required(),
])
->required()
]);
}
public static function table(Table $table): Table
{
return $table
->headerActions([
CreateAction::make()
->form([
TextInput::make('name')
->required()
->maxLength(255),
// ...
]),
\Filament\Tables\Actions\ExportAction::make()
->exporter(PatientExporter::class)
// ->fileDisk(null)
// ->fileDisk('public')
->formats([
ExportFormat::Csv,
])
->fileName(fn (Export $export): string => "patient-{$export->getKey()}.csv"),
])
->columns([
Tables\Columns\TextColumn::make('name')
->searchable(),
Tables\Columns\TextColumn::make('type'),
Tables\Columns\TextColumn::make('date_of_birth')
->sortable(),
Tables\Columns\TextColumn::make('owner.name')
->searchable(),
])
->filters([
//
Tables\Filters\SelectFilter::make('type')
->options([
'cat' => 'Cat',
'dog' => 'Dog',
'rabbit' => 'Rabbit',
]),
])
->actions([
Tables\Actions\EditAction::make(),
// CreateAction::make()
// ->model(Patient::class)
// ->form([
// TextInput::make('name')
// ->required()
// ->maxLength(255),
// // ...
// ]),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
public static function getRelations(): array
{
return [
//
RelationManagers\TreatmentsRelationManager::class,
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListPatients::route('/'),
'create' => Pages\CreatePatient::route('/create'),
'edit' => Pages\EditPatient::route('/{record}/edit'),
];
}
// protected function getHeaderActions(): array
// {
// return [
// ExportAction::make()->exports([
// ExcelExport::make('patient')->fromTable(),
// ])
// ];
// }
}
开始导出
啥也没有,浏览器没有反应,导出个寂寞
3. 您期望得到的结果?
求各位大佬不吝赐教
filament 导出是基于队列的吧,你要先运行队列:
php artisan queue:work
如果已经运行了,检查你队列的执行情况。
如果不是队列的问题,filament 导出结果是通过消息通知获取的,面板是否开启了通知。再就是去storage目录看看是否生成了csv文件,excel是基于csv的。
“在浏览器中导出而不是在本地”。你这个描述就不准确,是希望达到前端操作,浏览器导出还是达到什么效果?如果浏览器没有动静,那么是否就需要去检查浏览器请求有没有错,如果没错,那就是在导出的地方有问题,这样问题不就定位到了吗?
官方的还没用过,我是直接用这个插件的https://filamentphp.com/plugins/pxlrbt-excel, 点导出后浏览器直接下载的
我的意思就是在浏览器中导出,而不是在laravel内部storage/app/public目录下导出
最后去官网找的插件 filamentphp.com/plugins/pxlrbt-exc... 安装上面的步骤操作就好了 但是导出excel数据稍微多一点就不行了 5w条硬是导不出来,后来使用了laravel excel 但是导出要十几秒,有没有可以提升速度的 这是导出文件
这是资源控制器
如果超过5万,建议用xlswriter或者golang导出,