{"id":22591481,"url":"https://github.com/netsells/exportable-package","last_synced_at":"2025-03-28T18:25:48.728Z","repository":{"id":52117216,"uuid":"165037246","full_name":"netsells/exportable-package","owner":"netsells","description":"Reusable code to export Eloquent model data to PDF or CSV formats","archived":false,"fork":false,"pushed_at":"2021-05-07T09:32:17.000Z","size":12,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-02T18:51:42.304Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/netsells.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-01-10T10:11:01.000Z","updated_at":"2021-05-07T09:32:20.000Z","dependencies_parsed_at":"2022-09-08T06:40:55.883Z","dependency_job_id":null,"html_url":"https://github.com/netsells/exportable-package","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/netsells%2Fexportable-package","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/netsells%2Fexportable-package/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/netsells%2Fexportable-package/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/netsells%2Fexportable-package/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/netsells","download_url":"https://codeload.github.com/netsells/exportable-package/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246078309,"owners_count":20720131,"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":[],"created_at":"2024-12-08T09:12:54.250Z","updated_at":"2025-03-28T18:25:48.625Z","avatar_url":"https://github.com/netsells.png","language":"PHP","readme":"# Exportable\n[![Latest Version](https://img.shields.io/github/release/netsells/exportable.svg?style=flat-square)](https://github.com/netsells/exportable-package/releases)\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)\n[![Total Downloads](https://img.shields.io/packagist/dt/netsells/exportable.svg?style=flat-square)](https://github.com/netsells/exportable-package)\n\nAllows easy exporting of data from Eloquent models\n\nIt is created and maintained by the [Netsells team](https://netsells.co.uk/)\n\n## Getting Started\n\nInstall `Exportable` using Composer.\n\n```\n$ composer require netsells/exportable\n```\n\n### Implementation\n\nThis implementation of Exportable makes exporting data from Eloquent models to CSV or PDF formats simple. Simply extend the `Netsells\\Exportable\\ExportableModel` base class included in the package for any Eloquent model that needs to use the export functionality.\n\n```\nuse Netsells\\Exportable\\ExportableModel;\n\nclass User extends ExportableModel\n```\n\n#### ExportableModel ####\nThe base class for the exportable model implements and satisfies the `ExportableContract` interface that can be applied to other classes. The two traits that perform the exporting of data are imported here as well.\n\n```\nuse Netsells\\Exportable\\Contracts\\ExportableContract;\nuse Netsells\\Exportable\\Traits\\ExportableCsv;\nuse Netsells\\Exportable\\Traits\\ExportablePdf;\n\nclass ExportableModel extends Model implements ExportableContract\n{\n    use ExportablePdf;\n    use ExportableCsv;\n```\n\nThe two core methods for exporting data are as follows:\n\n`public static function exportToPdf`\n\n`public static function exportToCsv`\n\nThe remaining two methods in the class are used to build the layout closure used by Csvme to export the data to CSV format and retrieve constants safely should they be used to provide routes to config or views.\n\n`public static function getLayoutClosure`\n\n`public static function getConstant`\n\n\n### Basic Usage ###\n\n#### PDF ####\n\nExporting data to PDF format requires a view to format the output, the data to be exported and a list of headers. An example view is included with the package and custom views can easily be applied where desired.\n\n```php\n  $users = User::all()-\u003etoArray();\n  $headers = [\n      \"first_name\"    =\u003e \"First Name\",\n      \"last_name\"     =\u003e \"Last Name\",\n      \"email\"         =\u003e \"Email\",\n      \"phone\"         =\u003e \"Phone\",\n  ];\n\n  $view = \"exportable\\\\pdf\";\n  User::exportToPdf($view, $users, $headers);\n```\n#### CSV ####\n\nExporting data to CSV format simply needs the data and headers, if any, that are needed for the output. No special formatting is required since it is a CSV.\n\n```php\n  $users = User::all()-\u003etoArray();\n  $headers = [\n      \"first_name\"    =\u003e \"First Name\",\n      \"last_name\"     =\u003e \"Last Name\",\n      \"email\"         =\u003e \"Email\",\n      \"phone\"         =\u003e \"Phone\",\n  ];\n\n  User::exportToCsv($users, $headers);\n```\n\n### Advanced Usage ###\nIncluded in the package is a `exportable.php` file that contains the bare bones to set up data that can be used to set up the export to CSV or PDF formats. This can be modified to suit your requirements, with multiple sections for different exports.\n\n```php\n  return [\n    'pdf' =\u003e [\n        'headers' =\u003e [\n            // Map headers for PDF export here\n        ],\n        // View used to construct PDF export\n        'view' =\u003e 'exportable\\pdf',\n    ],\n    'csv' =\u003e [\n        'headers' =\u003e [\n            // Map headers for CSV export here\n        ],\n    ]\n  ];\n```\n##### Example #####\n```php\n  return [\n    'user' =\u003e [\n      'pdf' =\u003e [\n          'headers' =\u003e [\n            \"first_name\"    =\u003e \"First Name\",\n            \"last_name\"     =\u003e \"Last Name\",\n            \"email\"         =\u003e \"Email\",\n            \"phone\"         =\u003e \"Phone\",\n            \"email\"         =\u003e \"Email\",\n          ],\n          // View used to construct PDF export\n          'view' =\u003e 'exportable\\user_pdf',\n      ],\n    ],\n    'admin' =\u003e [\n      'pdf' =\u003e [\n          'headers' =\u003e [\n            \"first_name\"    =\u003e \"First Name\",\n            \"last_name\"     =\u003e \"Last Name\",\n            \"email\"         =\u003e \"Email\",\n            \"phone\"         =\u003e \"Phone\",\n            \"email\"         =\u003e \"Email\",\n            \"permission_level\" =\u003e \"Permissions\",\n          ],\n          // View used to construct PDF export\n          'view' =\u003e 'exportable\\admin_pdf',\n      ],\n    ]  \n  ];\n```\nFor simplicity you can add the paths to each config as constants on the model.\n\n```php\n  class User extends ExportableModel\n  {\n      const ADMIN_PDF_CONFIG = 'exportable.admin.pdf';\n      const USER_PDF_CONFIG = 'exportable.user.pdf';\n```\nUsing the `getConstant` static method, the value of the constant can be retrieved. This method returns null if the constant is not found on the class. Using the value of the constant, the required config data can be used to export the data, as shown below.\n\n```php\n  // Confirm class constant defined\n  if ($configPath = User::getConstant('ADMIN_PDF_CONFIG')) {\n      $config = Config::get($configPath);\n      $headers = $config['headers'];\n      $view = $config['view'];\n      $users = User::all()-\u003etoArray();\n      User::exportToPdf($view, $data, $headers);\n  }\n```\n\n\n## Built With\n\n* [Csvme](https://github.com/netsells/csvme) - An opinionated library that utilises the league/csv library\n* [Dompdf](https://github.com/dompdf/dompdf) - Dompdf is an HTML to PDF converter\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnetsells%2Fexportable-package","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnetsells%2Fexportable-package","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnetsells%2Fexportable-package/lists"}