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()
fiament 导出excel,显示后台正在导出,浏览器没有下载
参考链接: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(),

// ])

// ];

// }

}

开始导出

fiament 导出excel,显示后台正在导出,浏览器没有下载

啥也没有,浏览器没有反应,导出个寂寞:sob: :sob: :sob:

3. 您期望得到的结果?

求各位大佬不吝赐教

《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 9

filament 导出是基于队列的吧,你要先运行队列:php artisan queue:work
如果已经运行了,检查你队列的执行情况。

如果不是队列的问题,filament 导出结果是通过消息通知获取的,面板是否开启了通知。再就是去storage目录看看是否生成了csv文件,excel是基于csv的。

5个月前 评论

“在浏览器中导出而不是在本地”。你这个描述就不准确,是希望达到前端操作,浏览器导出还是达到什么效果?如果浏览器没有动静,那么是否就需要去检查浏览器请求有没有错,如果没错,那就是在导出的地方有问题,这样问题不就定位到了吗?

5个月前 评论

官方的还没用过,我是直接用这个插件的https://filamentphp.com/plugins/pxlrbt-excel, 点导出后浏览器直接下载的

5个月前 评论

我的意思就是在浏览器中导出,而不是在laravel内部storage/app/public目录下导出

5个月前 评论
echoyl 5个月前

最后去官网找的插件 filamentphp.com/plugins/pxlrbt-exc... 安装上面的步骤操作就好了 但是导出excel数据稍微多一点就不行了 5w条硬是导不出来,后来使用了laravel excel 但是导出要十几秒,有没有可以提升速度的 这是导出文件

<?php

namespace App\Filament\Exports;

use App\Models\Patient;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;

class FormExport  implements FromCollection, WithHeadings, WithMapping
{

    public function collection()
    {
        $data = [];

        // 使用 chunkById 方法分批处理数据
        Patient::with(['owner:id,name'])->orderBy('id')->chunkById(500, function ($patients) use (&$data) {
            foreach ($patients as $patient) {
                $data[] = [
                    'id' => $patient->id,
                    'name' => $patient->name,
                    'type' => $patient->type,
                    'date_of_birth' => $patient->date_of_birth,
                    'owner' => $patient->owner->name,
                    'created_at' => $patient->created_at,
                    'updated_at' => $patient->updated_at,
                ];
            }
        });

        return collect($data);
    }

    public function headings(): array
    {
        return [
            'ID',
            '姓名',
            '类型',
            '生日',
            '主人',
            '创建时间',
            '修改时间',
            // 添加其他字段
        ];
    }

    public function map($row): array
    {
        return [
            $row['id'],
            $row['name'],
            $row['type'],
            $row['date_of_birth'],
            $row['owner'],
            $row['created_at'],
            $row['updated_at'],
            // 添加其他字段
        ];
    }
    public static function getModel(): string
    {
        return Patient::class;
    }



}

这是资源控制器

    public static function table(Table $table): Table

    {
        set_time_limit(60);
        return $table
            ->headerActions([

                CreateAction::make()
                    ->form([
                        TextInput::make('name')
                            ->required()
                            ->maxLength(255),
                    ]),
                Tables\Actions\Action::make('exportCSV')
                    ->label('导出CSV')
                    ->icon('heroicon-m-pencil-square')
                    ->color('success')
                    ->action(function () {
                            return Excel::download(new FormExport(), date('Y-m-d H:i:s').self::$navigationLabel.'.csv');
                    }),
5个月前 评论
laravel_v 5个月前
蜗牛啊蜗牛 (作者) (楼主) 5个月前

如果超过5万,建议用xlswriter或者golang导出,

4个月前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!