Download the PHP package zegitz/laravel-csv-response without Composer
On this page you can find all versions of the php package zegitz/laravel-csv-response. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download zegitz/laravel-csv-response
More information about zegitz/laravel-csv-response
Files in zegitz/laravel-csv-response
Package laravel-csv-response
Short Description Add a CSV response type to Laravel
License MIT
Informations about the package laravel-csv-response
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.
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.
Installation
Require this package with composer using the following command:
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
.
Usage
Base data format
The csv()
method is very flexible about data format. All this examples return exactly the same response.
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:
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.
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.
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.
All versions of laravel-csv-response with dependencies
illuminate/routing Version 5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^7.0|^8.0
illuminate/support Version 5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^7.0|^8.0