{"id":17160966,"url":"https://github.com/audunru/export-response","last_synced_at":"2025-03-21T05:14:30.894Z","repository":{"id":45926911,"uuid":"407671897","full_name":"audunru/export-response","owner":"audunru","description":"Export JSON responses from Laravel to CSV","archived":false,"fork":false,"pushed_at":"2024-05-20T18:31:00.000Z","size":293,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-05-21T08:57:02.611Z","etag":null,"topics":["csv","export","json","laravel"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/audunru.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-09-17T20:30:10.000Z","updated_at":"2024-07-16T09:03:45.193Z","dependencies_parsed_at":"2024-05-20T08:45:21.441Z","dependency_job_id":"b3be9076-c159-4100-9d01-4773c25ad3df","html_url":"https://github.com/audunru/export-response","commit_stats":{"total_commits":7,"total_committers":1,"mean_commits":7.0,"dds":0.0,"last_synced_commit":"7ddfb630557965d88159d6aad3e1bc142fa93c96"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/audunru%2Fexport-response","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/audunru%2Fexport-response/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/audunru%2Fexport-response/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/audunru%2Fexport-response/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/audunru","download_url":"https://codeload.github.com/audunru/export-response/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244739950,"owners_count":20501992,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csv","export","json","laravel"],"created_at":"2024-10-14T22:26:40.677Z","updated_at":"2025-03-21T05:14:30.731Z","avatar_url":"https://github.com/audunru.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Export JSON responses from Laravel\n\n[![Build Status](https://github.com/audunru/export-response/actions/workflows/validate.yml/badge.svg)](https://github.com/audunru/export-response/actions/workflows/validate.yml)\n[![Coverage Status](https://coveralls.io/repos/github/audunru/export-response/badge.svg?branch=master)](https://coveralls.io/github/audunru/export-response?branch=master)\n[![StyleCI](https://github.styleci.io/repos/407671897/shield?branch=master)](https://github.styleci.io/repos/407671897)\n\nCurrently supported:\n\n- CSV\n- XLSX\n- XML\n\n# Installation\n\n## Step 1: Install with Composer\n\n```bash\ncomposer require audunru/export-response\n```\n\nDepending on which formats you want to export to, you will have to install additional packages:\n\n| Format | Package                                                       |\n| ------ | ------------------------------------------------------------- |\n| CSV    | [spatie/simple-excel](https://github.com/spatie/simple-excel) |\n| XLSX   | [spatie/simple-excel](https://github.com/spatie/simple-excel) |\n| XML    | [spatie/array-to-xml](https://github.com/spatie/array-to-xml) |\n\n## Step 2: Add middleware to your routes\n\nTo allow exports for all your API endpoints, add middleware to `Kernel.php`:\n\n```php\n'api' =\u003e [\n    'throttle:api',\n    \\Illuminate\\Routing\\Middleware\\SubstituteBindings::class,\n    \\audunru\\ExportResponse\\Middleware\\ExportCsv::class,\n    \\audunru\\ExportResponse\\Middleware\\ExportXlsx::class,\n    \\audunru\\ExportResponse\\Middleware\\ExportXml::class,\n],\n```\n\nTo add it to one particular API resource, you can use this in `api.php`:\n\n```php\nRoute::apiResource('documents', DocumentController::class)\n    -\u003emiddleware([\n        ExportCsv::class,\n        ExportXlsx::class,\n        ExportXml::class\n    ])\n    -\u003ename('documents');\n```\n\nYou can specify an array key which will be used to retrieve the data. \"Dot\" notation is supported.\n\n```php\nRoute::apiResource('documents', DocumentController::class)\n    -\u003emiddleware([\n        ExportCsv::with([\n            'key' =\u003e 'data',\n        ]),\n        ExportXlsx::with([\n            'key' =\u003e 'data',\n        ]),\n        ExportXml::class::with([\n            'key' =\u003e 'data',\n        ]),\n    ])\n    -\u003ename('documents');\n```\n\nYou can also add the middleware to the `$middlewareGroups` and `$routeMiddleware` arrays in `app/Http/Kernel.php`:\n\n```php\nprotected $middlewareGroups = [\n    'web' =\u003e [\n        \\App\\Http\\Middleware\\EncryptCookies::class,\n        \\Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse::class,\n        \\Illuminate\\Session\\Middleware\\StartSession::class,\n        \\Illuminate\\View\\Middleware\\ShareErrorsFromSession::class,\n        \\App\\Http\\Middleware\\VerifyCsrfToken::class,\n        \\Illuminate\\Routing\\Middleware\\SubstituteBindings::class,\n    ],\n\n    'api' =\u003e [\n        'throttle:api',\n        \\Illuminate\\Routing\\Middleware\\SubstituteBindings::class,\n        // Add ExportCsv middleware to all requests. \"data\" is the name of\n        // the key to retrieve using \"dot\" notation.\n        'csv:data',\n    ],\n];\n\nprotected $routeMiddleware = [\n    'auth' =\u003e \\App\\Http\\Middleware\\Authenticate::class,\n    'auth.basic' =\u003e \\Illuminate\\Auth\\Middleware\\AuthenticateWithBasicAuth::class,\n    'cache.headers' =\u003e \\Illuminate\\Http\\Middleware\\SetCacheHeaders::class,\n    'can' =\u003e \\Illuminate\\Auth\\Middleware\\Authorize::class,\n    'guest' =\u003e \\App\\Http\\Middleware\\RedirectIfAuthenticated::class,\n    'password.confirm' =\u003e \\Illuminate\\Auth\\Middleware\\RequirePassword::class,\n    'signed' =\u003e \\Illuminate\\Routing\\Middleware\\ValidateSignature::class,\n    'throttle' =\u003e \\Illuminate\\Routing\\Middleware\\ThrottleRequests::class,\n    'verified' =\u003e \\Illuminate\\Auth\\Middleware\\EnsureEmailIsVerified::class,\n    'csv' =\u003e \\audunru\\ExportResponse\\Middleware\\ExportCsv::class,\n];\n```\n\n### Exporting from controller\n\nInstead of using middleware, you can perform the export in the controller:\n\n```php\nclass ProductController extends Controller\n{\n    public function csv()\n    {\n        $products = Product::all();\n\n        return $products-\u003etoCsv('filename.csv');\n    }\n```\n\nLazy collections are also supported:\n\n```php\nclass ProductController extends Controller\n{\n    public function csv()\n    {\n        $products = Product::lazy();\n\n        return $products-\u003etoCsv('filename.csv');\n    }\n```\n\nPlease use lazy collections when you can. During testing, using `Product::lazy()` to export 10,000 products took about 2MB of memory, compared to 44 MB of memory using `Product::all()`. Both exports took the same amount of time (around 45 seconds).\n\n## Step 3: Exporting a response\n\nIn order to retrieve an API response as CSV instead of JSON, send a request to your API with the `Accept` header set to `text/csv`.\n\nFor XML, set the header to `application/xml`.\n\nFor XLSX, set the header to `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet`.\n\n# Configuration\n\nPublish the configuration file by running:\n\n```php\nphp artisan vendor:publish --tag=export-response-config\n```\n\n# Development\n\n## Testing\n\nRun tests:\n\n```bash\ncomposer test\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faudunru%2Fexport-response","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faudunru%2Fexport-response","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faudunru%2Fexport-response/lists"}