{"id":18474183,"url":"https://github.com/usmanhalalit/laracsv","last_synced_at":"2025-05-15T12:05:26.506Z","repository":{"id":54171712,"uuid":"88727831","full_name":"usmanhalalit/laracsv","owner":"usmanhalalit","description":"CSV files from Eloquent model in seconds - a Laravel package.","archived":false,"fork":false,"pushed_at":"2021-03-05T14:59:19.000Z","size":64,"stargazers_count":608,"open_issues_count":5,"forks_count":58,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-04-14T22:18:34.532Z","etag":null,"topics":["csv","eloquent","excel","export","laravel"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/usmanhalalit.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"usmanhalalit"}},"created_at":"2017-04-19T09:40:32.000Z","updated_at":"2025-03-02T12:16:16.000Z","dependencies_parsed_at":"2022-08-13T08:20:12.912Z","dependency_job_id":null,"html_url":"https://github.com/usmanhalalit/laracsv","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/usmanhalalit%2Flaracsv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/usmanhalalit%2Flaracsv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/usmanhalalit%2Flaracsv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/usmanhalalit%2Flaracsv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/usmanhalalit","download_url":"https://codeload.github.com/usmanhalalit/laracsv/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254337613,"owners_count":22054253,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csv","eloquent","excel","export","laravel"],"created_at":"2024-11-06T10:28:25.443Z","updated_at":"2025-05-15T12:05:21.494Z","avatar_url":"https://github.com/usmanhalalit.png","language":"PHP","funding_links":["https://github.com/sponsors/usmanhalalit"],"categories":["Developer Tools \u0026 Libraries"],"sub_categories":["🚀 How to contribute"],"readme":"# LaraCSV\n\nA Laravel package to easily generate CSV files from Eloquent model.\n\n[![Build Status](https://travis-ci.org/usmanhalalit/laracsv.svg?branch=master)](https://travis-ci.org/usmanhalalit/laracsv)\n[![Total Downloads](https://poser.pugx.org/usmanhalalit/laracsv/downloads)](https://packagist.org/packages/usmanhalalit/laracsv)\n[![Daily Downloads](https://poser.pugx.org/usmanhalalit/laracsv/d/daily)](https://packagist.org/packages/usmanhalalit/laracsv)\n\n## Basic usage\n\n```php\n$users = User::get(); // All users\n$csvExporter = new \\Laracsv\\Export();\n$csvExporter-\u003ebuild($users, ['email', 'name'])-\u003edownload();\n```\n\nAnd a proper CSV file will be downloaded with `email` and `name` fields. As simple as it sounds!\n\n## Installation\n\nJust run this on your terminal:\n\n```\ncomposer require usmanhalalit/laracsv:^2.1\n```\nand you should be good to go.\n\n## Full Documentation\n\n - [Build CSV](#build-csv)\n - [Output Options](#output-options)\n    - [Download](#download)\n - [Custom Headers](#custom-headers)\n    - [No Header](#no-header)\n - [Modify or Add Values](#modify-or-add-values)\n    - [Add fields and values](#add-fields-and-values)\n - [Model Relationships](#model-relationships)\n - [Build by chunks](#build-by-chunks)\n\n\n### Build CSV\n\n`$exporter-\u003ebuild($modelCollection, $fields)` takes three parameters.\nFirst one is the model (collection of models), seconds one takes the field names\n you want to export, third one is config, which is optional.\n\n```php\n$csvExporter-\u003ebuild(User::get(), ['email', 'name', 'created_at']);\n```\n\n### Output Options\n#### Download\n\nTo get file downloaded to the browser:\n```php\n$csvExporter-\u003edownload();\n```\n\nYou can provide a filename if you wish:\n```php\n$csvExporter-\u003edownload('active_users.csv');\n```\nIf no filename is given a filename with date-time will be generated.\n\n#### Advanced Outputs\n\nLaraCSV uses [League CSV](http://csv.thephpleague.com/). You can do what League CSV\nis able to do. You can get the underlying League CSV writer and reader instance by calling:\n\n```php\n$csvWriter = $csvExporter-\u003egetWriter();\n$csvReader = $csvExporter-\u003egetReader();\n```\n\nAnd then you can do several things like:\n```php\n$csvString = $csvWriter-\u003egetContent(); // To get the CSV as string\n$csvReader-\u003ejsonSerialize(); // To turn the CSV in to an array\n```\n\nFor more information please check [League CSV documentation](http://csv.thephpleague.com/).\n\n\n### Custom Headers\n\nAbove code example will generate a CSV with headers email, name, created_at and corresponding rows after.\n\nIf you want to change the header with a custom label just pass it as array value:\n```php\n$csvExporter-\u003ebuild(User::get(), ['email', 'name' =\u003e 'Full Name', 'created_at' =\u003e 'Joined']);\n```\n\nNow `name` column will show the header `Full Name` but it will still take\nvalues from `name` field of the model.\n\n#### No Header\n\nYou can also suppress the CSV header:\n```php\n$csvExporter-\u003ebuild(User::get(), ['email', 'name', 'created_at'], [\n    'header' =\u003e false,\n]);\n```\n\n### Modify or Add Values\n\nThere is a hook which is triggered before processing a database row.\n  For example, if you want to change the date format you can do so.\n```php\n$csvExporter = new \\Laracsv\\Export();\n$users = User::get();\n\n// Register the hook before building\n$csvExporter-\u003ebeforeEach(function ($user) {\n    $user-\u003ecreated_at = date('f', strtotime($user-\u003ecreated_at));\n});\n\n$csvExporter-\u003ebuild($users, ['email', 'name' =\u003e 'Full Name', 'created_at' =\u003e 'Joined']);\n```\n\n**Note:** If a `beforeEach` callback returns `false` then the entire row will be\nexcluded from the CSV. It can come handy to filter some rows.\n\n#### Add fields and values\n\nYou may also add fields that don't exists in a database table add values on the fly:\n\n```php\n// The notes field doesn't exist so values for this field will be blank by default\n$csvExporter-\u003ebeforeEach(function ($user) {\n    // Now notes field will have this value\n    $user-\u003enotes = 'Add your notes';\n});\n\n$csvExporter-\u003ebuild($users, ['email', 'notes']);\n```\n\n### Model Relationships\n\nYou can also add fields in the CSV from related database tables, given the model\n has relationships defined.\n\nThis will get the product title and the related category's title (one to one):\n```php\n$csvExporter-\u003ebuild($products, ['title', 'category.title']);\n```\n\nYou may also tinker relation things as you wish with hooks:\n\n```php\n$products = Product::with('categories')-\u003ewhere('order_count', '\u003e', 10)-\u003eorderBy('order_count', 'desc')-\u003eget();\n$fields = ['id', 'title','original_price' =\u003e 'Market Price', 'category_ids',];\n$csvExporter = new \\Laracsv\\Export();\n$csvExporter-\u003ebeforeEach(function ($product) {\n    $product-\u003ecategory_ids = implode(', ', $product-\u003ecategories-\u003epluck('id')-\u003etoArray());\n});\n```\n\n## Build by chunks\n\nFor larger datasets, which can become more memory consuming, a builder instance can be used to process the results in chunks. Similar to the row-related hook, a chunk-related hook can be used in this case for e.g. eager loading or similar chunk based operations. The behaviour between both hooks is similar; it gets called before each chunk and has the entire collection as an argument. **In case `false` is returned the entire chunk gets skipped and the code continues with the next one.**\n\n```$export = new Export();\n\n// Perform chunk related operations\n$export-\u003ebeforeEachChunk(function ($collection) {\n    $collection-\u003eload('categories');\n});\n\n$export-\u003ebuildFromBuilder(Product::select(), ['category_label']);\n```\n\nThe default chunk size is set to 1000 results but can be altered by passing a different value in the `$config` passed to `buildFromBuilder`. Example alters the chunk size to 500.\n\n```php\n// ...\n\n$export-\u003ebuildFromBuilder(Product::select(), ['category_label'], ['chunk' =\u003e 500]);\n```\n\n\u0026copy; [Muhammad Usman](http://usman.it/). Licensed under MIT license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fusmanhalalit%2Flaracsv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fusmanhalalit%2Flaracsv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fusmanhalalit%2Flaracsv/lists"}