Installation:
composer require usmanhalalit/laracsv:^2.1
No additional configuration is required—just autoload the package.
First Use Case: Export a basic CSV from an Eloquent collection:
use Laracsv\Export;
$exporter = new Export();
$exporter->build(User::all(), ['id', 'name', 'email'])->download('users.csv');
This generates a downloadable CSV with the specified columns.
Where to Look First:
Instantiation:
$exporter = new \Laracsv\Export();
Reuse the same instance for multiple exports (stateless).
Building CSV:
$exporter->build(
$collection, // Eloquent collection or array
['field1', 'field2'], // Fields to export
$config = [] // Optional: headers, chunking, etc.
);
Model::all(), Model::where()->get(), or raw arrays.$exporter->build(User::all(), [
'name',
'full_name' => function ($user) {
return "{$user->first_name} {$user->last_name}";
}
]);
Output Handling:
$exporter->download('filename.csv');
return $exporter->getResponse('filename.csv');
$exporter->save('path/to/file.csv');
Chunking for Large Datasets:
$exporter->build(User::chunk(1000), ['id', 'email'])->download();
Process records in chunks to avoid memory issues.
public function exportUsers()
{
return (new Export())->build(User::all(), ['id', 'name'])->getResponse('users.csv');
}
use Laracsv\Export;
class ExportUsersCommand extends Command
{
public function handle()
{
(new Export())->build(User::all(), ['id', 'email'])->save(storage_path('exports/users.csv'));
}
}
return (new Export())->build(User::cursor(), ['id', 'name'])->getResponse('users.csv');
Memory Limits:
Model::cursor() or chunking:
$exporter->build(User::cursor(), ['id', 'name'])->download();
chunk().Relationship Data:
user->posts) may not serialize correctly.with():
$users = User::with('posts')->get();
$exporter->build($users, [
'name',
'posts_count' => function ($user) {
return $user->posts->count();
}
]);
Special Characters:
escape config:
$exporter->build($users, ['description'], ['escape' => true]);
Timezone/Date Formatting:
$exporter->build(User::all(), [
'created_at' => function ($user) {
return $user->created_at->format('Y-m-d H:i:s');
}
]);
$exporter->save(storage_path('debug.csv'));
user_name vs. username) will silently fail. Validate with:
dd($user->getFillable()); // Check available fields
Custom Delimiters: Override the default comma delimiter:
$exporter->build($users, ['id', 'name'], ['delimiter' => ';']);
Custom Headers: Rename or skip headers:
$exporter->build($users, ['id' => 'User ID', 'name' => 'Full Name']);
Disable headers entirely:
$exporter->build($users, ['id', 'name'], ['headers' => false]);
Post-Processing: Modify values after export:
$exporter->build($users, ['name'])->after(function ($csv) {
// Manipulate the CSV string (e.g., add a footer)
return $csv . "\nExported on: " . now()->format('Y-m-d');
});
Events: Listen for export events (if extended):
Event::listen('laracsv.exporting', function ($exporter) {
// Log or modify before export
});
[
'headers' => true, // Show headers (default: true)
'delimiter' => ',', // CSV delimiter
'enclosure' => '"', // Field enclosure
'escape' => false, // Escape special characters
'line_ending' => "\n", // Line ending (use "\r\n" for Windows)
'strict' => true, // Throw errors on missing fields
]
$exporter->build($users, ['name'], ['encoding' => 'UTF-8']);
How can I help you explore Laravel packages today?