sinemacula/laravel-resource-exporter
Composer 安装命令:
composer require sinemacula/laravel-resource-exporter
包简介
A versatile Laravel package for exporting JsonResource and JsonResourceCollection objects into various formats with customizable drivers.
README 文档
README
Laravel Resource Exporter is a Laravel integration package for converting JsonResource and
ResourceCollection payloads into export-friendly formats.
The package includes:
- a manager (
ExportManager) for resolving configured drivers - built-in CSV and XML drivers
- a facade (
SineMacula\Exporter\Facades\Exporter) for convenient usage - extension hooks for custom export drivers without changing core package code
Features
- Export arrays, single resources, and resource collections
- Select a driver explicitly (
csv,xml) or use the configured default - Exclude fields consistently across drivers using
withoutFields() - Customize per-driver options through
config/exporter.php - Register custom drivers via
ExportManager::extend()
Supported Drivers
csvxml
Installation
To install the Laravel Resource Exporter, run the following command in your project directory:
composer require sinemacula/laravel-resource-exporter
Configuration
After installation, publish the package configuration:
php artisan vendor:publish --provider="SineMacula\Exporter\ExporterServiceProvider"
This creates config/exporter.php, where you can control:
default: the default exporter nameexporters: named exporters and theirdriver+ optionsalias: container/facade accessor alias (defaults toexporter)
Usage
Basic usage
use SineMacula\Exporter\Facades\Exporter; use App\Http\Resources\YourResource; // Export a single resource as CSV $csv = Exporter::format('csv')->exportItem(new YourResource($item)); // Export a collection as XML $xml = Exporter::format('xml')->exportCollection(YourResource::collection($collection));
Field exclusion
$csv = Exporter::format('csv') ->withoutFields(['internal_id', 'debug']) ->exportCollection(YourResource::collection($collection));
On-demand exporters
use SineMacula\Exporter\Facades\Exporter; // Build an exporter from ad-hoc config $exporter = Exporter::build([ 'driver' => 'csv', 'delimiter' => ';', ]);
Custom drivers
use SineMacula\Exporter\ExportManager; app(ExportManager::class)->extend('json', function ($app, array $config) { return new App\Exporters\JsonExporter($config); });
Testing
composer test
composer test-coverage
composer check
Contributing
Contributions are welcome via GitHub pull requests.
Security
If you discover a security issue, please contact Sine Macula directly rather than opening a public issue.
License
Licensed under the Apache License, Version 2.0.
统计信息
- 总下载量: 17.97k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 14
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2024-08-20