https://github.com/neoxia/laravel-csv-response
Add a CSV response type to Laravel
https://github.com/neoxia/laravel-csv-response
csv laravel php
Last synced: 3 months ago
JSON representation
Add a CSV response type to Laravel
- Host: GitHub
- URL: https://github.com/neoxia/laravel-csv-response
- Owner: neoxia
- License: mit
- Created: 2016-06-20T16:59:56.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2021-04-22T15:20:21.000Z (almost 5 years ago)
- Last Synced: 2025-10-08T19:02:43.503Z (6 months ago)
- Topics: csv, laravel, php
- Language: PHP
- Homepage:
- Size: 33.2 KB
- Stars: 15
- Watchers: 32
- Forks: 20
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://packagist.org/packages/neoxia/laravel-csv-response)
[](https://travis-ci.org/neoxia/laravel-csv-response)
[](https://coveralls.io/github/neoxia/laravel-csv-response?branch=master)
## Laravel CSV Response
This package adds a CSV response type to the Laravel `ResponseFactory` class. Because CSV is a data format, just like JSON, it should be possible to respond to a request with this format.
```PHP
$data = [
['first_name', 'last_name'],
['John', 'Doe'],
['Jane', 'Doe'],
];
return response()->csv($data);
```
This small package offers a straightforward solution that deals with conversion, from array or collection of objects to comma separated values string, and character encoding.
### Disclaimer
This package is just a pretty cool helper to create CSV responses without pain.
If you want to generate CSV (or Excel) files with a lot of options and more robustness you should take a look at [Maatwebsite/Laravel-Excel](https://github.com/Maatwebsite/Laravel-Excel).
## Installation
Require this package with composer using the following command:
```
composer require neoxia/laravel-csv-response
```
As of Laravel 5.5, this package will be automatically discovered and registered.
For older version of Laravel, add the service provider in `config/app.php`.
```PHP
Neoxia\Routing\ResponseFactoryServiceProvider::class,
```
## Usage
### Base data format
The `csv()` method is very flexible about data format. All this examples return exactly the same response.
```PHP
response()->csv(collect(
new User(['first_name' => 'John', 'last_name' => 'Doe']),
new User(['first_name' => 'Jane', 'last_name' => 'Doe']),
));
response()->csv([
['first_name', 'last_name'],
['John', 'Doe'],
['Jane', 'Doe'],
]);
response()->csv([
['first_name' => 'John', 'last_name' => 'Doe'],
['first_name' => 'Jane', 'last_name' => 'Doe'],
]);
response()->csv("first_name;last_name\r\nJohn;Doe\r\nJane;Doe");
```
### Objects as rows
If the "rows" of the array of data passed to the method are objects, you have to implement the `csvSerialize()` method in this objects. This method is based in the same principle than the `jsonSerialize()` method that is already implemented into an Eloquent model. It should return data as an associative array.
For example:
```PHP
class User
{
public function csvSerialize()
{
return [
'first_name' => $this->first_name,
'last_name' => $this->last_name,
];
}
}
```
### CSV first row
When the "rows" of the data collection are associative arrays or objects, the package use the keys of the first row to define the first row of the CSV response. This first row is generaly used as column titles in this type of file.
In order to have a consistent response, you have to be sure that every row in the data collection has the same number of values and the keys in the same order.
### Other parameters
The `csv()` function declaration, based on Laravel `json()` function, is the following.
```PHP
public function csv($data, $status = 200, array $headers = [], array $options = [])
```
#### Status
Typically, you should return your CSV with status *200 Ok* but you are allowed to be imaginative. Maybe are you building a full REST-CSV API ;)
#### Headers
The default headers for this response are the following but you can overwrite it.
```PHP
[
'Content-Type' => 'text/csv; charset=WINDOWS-1252',
'Content-Encoding' => 'WINDOWS-1252',
'Content-Transfer-Encoding' => 'binary',
'Content-Description' => 'File Transfer',
]
```
Note that the default charset and encoding are automatically overwrited if a custom encoding is specified in the options (see below).
#### Options
The last argument lets you define how the CSV is built and formated. We have defined a format that fits very well in most cases but the optimal configuration may depend on your environment (language, Microsoft Office version, etc.).
The default options are the following.
```PHP
[
'encoding' => 'WINDOWS-1252',
'delimiter' => ';',
'quoted' => true,
'include_header' => true,
]
```