PHPackages                             zegitz/laravel-csv-response - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. zegitz/laravel-csv-response

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

zegitz/laravel-csv-response
===========================

Add a CSV response type to Laravel

v1.1.8(5y ago)09.8k1MITPHPPHP &gt;=5.5.9

Since Jun 9Pushed 5y agoCompare

[ Source](https://github.com/zegitz/laravel-csv-response)[ Packagist](https://packagist.org/packages/zegitz/laravel-csv-response)[ RSS](/packages/zegitz-laravel-csv-response/feed)WikiDiscussions master Synced today

READMEChangelog (2)Dependencies (5)Versions (11)Used By (0)

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.

```
$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

[](#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
------------

[](#installation)

Require this package with composer using the following command:

```
composer require zegitz/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`.

```
Zegitz\Routing\ResponseFactoryServiceProvider::class,
```

Usage
-----

[](#usage)

### Base data format

[](#base-data-format)

The `csv()` method is very flexible about data format. All this examples return exactly the same response.

```
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

[](#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:

```
class User
{
    public function csvSerialize()
    {
        return [
            'first_name' => $this->first_name,
            'last_name' => $this->last_name,
        ];
    }
}
```

### CSV first row

[](#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

[](#other-parameters)

The `csv()` function declaration, based on Laravel `json()` function, is the following.

```
public function csv($data, $status = 200, array $headers = [], array $options = [])
```

#### Status

[](#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

[](#headers)

The default headers for this response are the following but you can overwrite it.

```
[
    '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

[](#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.

```
[
    'encoding' => 'WINDOWS-1252',
    'delimiter' => ';',
    'quoted' => true,
    'include_header' => true,
]
```

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 57.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~139 days

Recently: every ~198 days

Total

10

Last Release

2004d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/31864113?v=4)[zegitz](/maintainers/zegitz)[@zegitz](https://github.com/zegitz)

---

Top Contributors

[![alexandre-butynski](https://avatars.githubusercontent.com/u/671662?v=4)](https://github.com/alexandre-butynski "alexandre-butynski (31 commits)")[![zegitz](https://avatars.githubusercontent.com/u/31864113?v=4)](https://github.com/zegitz "zegitz (7 commits)")[![mathieutu](https://avatars.githubusercontent.com/u/11351322?v=4)](https://github.com/mathieutu "mathieutu (4 commits)")[![flaforgue](https://avatars.githubusercontent.com/u/7113154?v=4)](https://github.com/flaforgue "flaforgue (3 commits)")[![claytonrcarter](https://avatars.githubusercontent.com/u/1420419?v=4)](https://github.com/claytonrcarter "claytonrcarter (2 commits)")[![mtmail](https://avatars.githubusercontent.com/u/3727288?v=4)](https://github.com/mtmail "mtmail (1 commits)")[![nunomaduro](https://avatars.githubusercontent.com/u/5457236?v=4)](https://github.com/nunomaduro "nunomaduro (1 commits)")[![svenluijten](https://avatars.githubusercontent.com/u/11269635?v=4)](https://github.com/svenluijten "svenluijten (1 commits)")[![thomasruiz](https://avatars.githubusercontent.com/u/3023617?v=4)](https://github.com/thomasruiz "thomasruiz (1 commits)")[![backstageel](https://avatars.githubusercontent.com/u/1692858?v=4)](https://github.com/backstageel "backstageel (1 commits)")[![lasselehtinen](https://avatars.githubusercontent.com/u/1290186?v=4)](https://github.com/lasselehtinen "lasselehtinen (1 commits)")[![mpowell-atomic](https://avatars.githubusercontent.com/u/10661049?v=4)](https://github.com/mpowell-atomic "mpowell-atomic (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/zegitz-laravel-csv-response/health.svg)

```
[![Health](https://phpackages.com/badges/zegitz-laravel-csv-response/health.svg)](https://phpackages.com/packages/zegitz-laravel-csv-response)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3274.9M308](/packages/psalm-plugin-laravel)[watson/active

Laravel helper for recognising the current route, controller and action

3253.6M14](/packages/watson-active)[dragon-code/pretty-routes

Pretty Routes for Laravel

10058.7k4](/packages/dragon-code-pretty-routes)[zonneplan/laravel-module-loader

Module loader for Laravel

24118.4k](/packages/zonneplan-laravel-module-loader)[bjuppa/laravel-blog

Add blog functionality to your Laravel project

483.3k2](/packages/bjuppa-laravel-blog)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

255.2k](/packages/aedart-athenaeum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
