Skip to content
This repository was archived by the owner on Feb 4, 2026. It is now read-only.

Repository files navigation

Csvme

Latest VersionSoftware LicenseTotal Downloads

Csvme is an opinionated library that utilises the league/csv library.

It is created and maintained by the Netsells team

Install

Install Csvme using Composer.

$ composer require netsells/csvme

Usage

Basic Usage

Csvme always expects an array of objects and optionally a layout closure for the header row.

$csv = newCsvme();
$csv->withHeader(['ID', 'Total', 'Number of Items', 'Created At'])
->withLayout(function(Order$order) {
return [
$order->id,
$order->total,
$order->items->count(),
$order->created_at->format('d-m-Y'),
];
})
->withItems($orders)
->output();

CSV Composers

It is possible to use an external class to offload the layout of the CSV to a dedicated file.

$csv = newCsvme();
$csv->output(newOrderExportComposer($orders));
<?phpuseNetsells\Csvme\Csvme;
useNetsells\Csvme\CsvComposer;
class OrderExportComposer implements CsvComposer
{
/** * The orders. * * @var array */protected$orders;
/** * Create a new csv composer. * * @param array $orders * @return void */publicfunction__construct($orders)
{
$this->orders = $orders;
}
/** * Configure the CSV * * @param Csvme $csv * @return void */publicfunctioncompose(Csvme$csv)
{
$csv->withHeader(['ID', 'Total', 'Number of Items', 'Created At'])
->withLayout(function(Order$order) {
return [
$order->id,
$order->total,
$order->items->count(),
$order->created_at->format('d-m-Y'),
];
})
->withItems($this->orders);
}
}

CORS Headers

When using Csvme in api endpoints, you can add the Access-Control-Allow-Origin header using the setCorsHeader() method. The method defaults to allow all origins but a specific origin can be passed in as an argument in the following way.

$csv = newCsvme();
$csv->withHeader(['ID', 'Total', 'Number of Items', 'Created At'])
->withLayout(function(Order$order) {
return [
$order->id,
$order->total,
$order->items->count(),
$order->created_at->format('d-m-Y'),
];
})
->withItems($orders)
->setCorsHeader('https://test.com')
->output();

About

An opinionated library to make exporting data in CSV format super simple.

Resources

Stars

4 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages