fersot/model-export 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

fersot/model-export

Composer 安装命令:

composer require fersot/model-export

包简介

Export Eloquent queries, collections or arrays to Excel/CSV with a fluent API, column mapping, chunk processing and streaming downloads.

README 文档

README

Export Eloquent queries, Collections, or arrays to Excel or CSV with a fluent API, column mapping, transform callbacks, chunk processing, and streaming HTTP downloads.

Latest Version PHP Version License Tests

Installation

composer require fersot/model-export

Laravel auto-discovers the service provider and facade. Optionally publish the config:

php artisan vendor:publish --tag=model-export-config

Quick Start

use Fersot\ModelExport\Exporter;

// Download directly from a controller
return Exporter::from(User::query())->download('users.xlsx');

// Save to disk
Exporter::from(Order::where('status', 'paid'))->toExcel(storage_path('exports/orders.xlsx'));

// With column mapping
return Exporter::from(User::query())
    ->columns([
        ExportColumn::make('name')->label('Nombre'),
        ExportColumn::make('email')->label('Correo'),
        ExportColumn::make('created_at')->label('Registro')->transform(fn($v) => $v->format('d/m/Y')),
    ])
    ->download('usuarios.xlsx');

Sources

use Fersot\ModelExport\Exporter;

// Eloquent Builder
Exporter::from(User::where('active', 1));

// Illuminate Collection
Exporter::from(collect([['name' => 'Alice'], ['name' => 'Bob']]));

// Plain PHP array
Exporter::from([['product' => 'Widget', 'price' => 9.99]]);

// Model shortcut (uses Model::query())
Exporter::model(User::class);

Fluent Options

Column Mapping

Define columns explicitly with a custom label and optional value transformer:

use Fersot\ModelExport\ExportColumn;

Exporter::from(User::query())
    ->columns([
        ExportColumn::make('name')->label('Nombre'),
        ExportColumn::make('email')->label('Correo Electrónico'),
        ExportColumn::make('age')->label('Edad')->transform(fn($v) => "{$v} años"),

        // The transform also receives the full row as a second argument
        ExportColumn::make('name')->label('Info')
            ->transform(fn($v, $row) => "{$v} <{$row['email']}>"),
    ])
    ->download('users.xlsx');

When ->columns() is used, only the specified columns are exported in the exact order defined.

Filter Columns (without mapping)

// Keep only these columns
Exporter::from(User::query())->only(['name', 'email'])->toArray();

// Exclude columns
Exporter::from(User::query())->except(['password', 'remember_token'])->toArray();

Chunk Processing (large datasets)

Exporter::from(User::query())
    ->chunk(500)           // default: 1000
    ->download('all_users.xlsx');

Output Formats

$exporter = Exporter::from(User::query());

// PHP array / Collection
$exporter->toArray();
$exporter->toCollection();

// Save to file
$exporter->toExcel('path/to/output.xlsx');   // returns the path
$exporter->toCsv('path/to/output.csv');      // returns the path

// HTTP download (StreamedResponse)
$exporter->download('filename.xlsx');
$exporter->downloadCsv('filename.csv');

Macros

After the service provider boots, macros are available on EloquentBuilder and Collection:

// Builder macros
User::where('active', 1)->toExcel(storage_path('exports/active_users.xlsx'));
User::where('active', 1)->toCsv(storage_path('exports/active_users.csv'));

// Return a download response directly
return User::query()->exportDownload('users.xlsx');
return User::query()->exportDownloadCsv('users.csv');

// Collection macros
$collection->toExcel('path/to/file.xlsx');
return $collection->exportDownload('data.xlsx');

HasExport Trait

Add the trait to any Eloquent model for convenient export methods:

use Fersot\ModelExport\Traits\HasExport;

class User extends Model
{
    use HasExport;
}

// Export all records
return User::export()->only(['name', 'email'])->download('users.xlsx');

// Export with a scoped query
return User::exportQuery(fn($q) => $q->where('role', 'admin'))
    ->download('admins.xlsx');

Configuration

After publishing, edit config/model-export.php:

return [
    'chunk_size'     => 1000,
    'disk'           => 'local',
    'default_format' => 'xlsx',
    'temp_path'      => null,   // null = sys_get_temp_dir()
];

Requirements

Dependency Version
PHP ^8.1
Laravel ^9.0 | ^10.0 | ^11.0 | ^12.0
fersot/excel-to ^2.0

Testing

composer install
./vendor/bin/phpunit --testdox

22 tests covering all features.

Author

Hember Colmenareshemberfer@gmail.com · GitHub

Support ☕️

Buy Me a Coffee

License

MIT — see LICENSE.

fersot/model-export 适用场景与选型建议

fersot/model-export 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「excel」 「csv」 「export」 「laravel」 「download」 「collection」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 fersot/model-export 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 fersot/model-export 我们能提供哪些服务?
定制开发 / 二次开发

基于 fersot/model-export 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 38
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-26