{"id":19621616,"url":"https://github.com/pkg6/think-csv","last_synced_at":"2026-03-17T16:08:24.563Z","repository":{"id":62548132,"uuid":"488375668","full_name":"pkg6/think-csv","owner":"pkg6","description":"一个thinkphp包，可以轻松地从Eloquent模型生成CSV文件。","archived":false,"fork":false,"pushed_at":"2023-08-08T03:43:55.000Z","size":4,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-01-09T11:40:57.564Z","etag":null,"topics":[],"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/pkg6.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-05-03T22:03:22.000Z","updated_at":"2023-04-04T04:05:20.000Z","dependencies_parsed_at":"2024-11-11T11:26:39.099Z","dependency_job_id":"d339fa4b-3adc-48a3-9c5d-e4a7b16abc04","html_url":"https://github.com/pkg6/think-csv","commit_stats":null,"previous_names":["tp5er/think-csvs"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pkg6%2Fthink-csv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pkg6%2Fthink-csv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pkg6%2Fthink-csv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pkg6%2Fthink-csv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pkg6","download_url":"https://codeload.github.com/pkg6/think-csv/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240917723,"owners_count":19878308,"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-11-11T11:23:44.661Z","updated_at":"2025-10-25T22:45:55.392Z","avatar_url":"https://github.com/pkg6.png","language":"PHP","readme":"\n## 基本用法\n\n~~~\n$users = User::select(); // All users\n$csvExporter = new \\tp5er\\thinkCsv\\Export();\n$csvExporter-\u003ebuild($users, ['email', 'name'])-\u003edownload();\n~~~\n\n## 建立CSV\n\n`$exporter-\u003ebuild($modelCollection, $fields)`需要三个参数。第一个是模型（模型的集合），第二个是要导出的字段名称，第三个是配置，这是可选的。\n\n~~~\n$csvExporter-\u003ebuild(User::select(), ['email', 'name', 'created_at']);\n~~~\n\n## 输出选项\n\n### 下载\n\n要将文件下载到浏览器:\n\n~~~\n$csvExporter-\u003edownload();\n~~~\n\n如果您愿意，可以提供文件名：\n\n~~~\n$csvExporter-\u003edownload('active_users.csv');\n~~~\n\n如果没有给出文件名，则将生成带有日期时间的文件名。\n\n###  高级输出\n\nLaraCSV 使用[League CSV](http://csv.thephpleague.com/)。您可以做 League CSV 能做的事情。您可以通过调用获取底层 League CSV writer 和 reader 实例：\n\n~~~\n$csvWriter = $csvExporter-\u003egetWriter();\n$csvReader = $csvExporter-\u003egetReader();\n~~~\n\n然后你可以做几件事，比如：\n\n~~~\n$csvString = $csvWriter-\u003egetContent(); // To get the CSV as string\n$csvReader-\u003ejsonSerialize(); // To turn the CSV in to an array\n~~~\n\n有关更多信息，请查看[League CSV 文档](http://csv.thephpleague.com/)。\n\n## 自定义标题\n\n上面的代码示例将生成一个带有标题电子邮件、名称、created_at 和后面的相应行的 CSV。\n\n如果要使用自定义标签更改标题，只需将其作为数组值传递：\n\n~~~\n$csvExporter-\u003ebuild(User::select(), ['email', 'name' =\u003e 'Full Name', 'created_at' =\u003e 'Joined']);\n~~~\n\n现在`name`列将显示标题，`Full Name`但它仍然会从`name`模型的字段中获取值。\n\n###  无标题\n\n您还可以取消 CSV 标头：\n\n~~~\n$csvExporter-\u003ebuild(User::select(), ['email', 'name', 'created_at'], [\n    'header' =\u003e false,\n]);\n~~~\n\n## 修改或添加值\n\n在处理数据库行之前会触发一个钩子。例如，如果您想更改日期格式，您可以这样做。\n\n~~~\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**注意：**如果`beforeEach`回调返回，`false`则整行将从 CSV 中排除。过滤一些行会很方便。\n\n###  添加字段和值\n\n您还可以添加数据库表中不存在的字段并动态添加值：\n\n~~~\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## 分块构建\n\n对于可能会消耗更多内存的较大数据集，可以使用构建器实例以块的形式处理结果。类似于行相关的钩子，在这种情况下可以使用块相关的钩子，例如急切加载或类似的基于块的操作。两个钩子之间的行为是相似的；它在每个块之前被调用，并将整个集合作为参数。**如果`false`返回，整个块被跳过，代码继续下一个。**\n\n~~~\n// Perform chunk related operations\n$export-\u003ebeforeEachChunk(function ($collection) {\n   \n});\n$export-\u003ebuildFromBuilder(User::newQuery(),['email', 'name']);\n$export-\u003ebuildFromBuilder(Db::table('user'),['email', 'name']);\n~~~\n\n默认块大小设置为 1000 个结果，但可以通过在`$config`传递给`buildFromBuilder`. 示例将块大小更改为 500。\n\n~~~\n$export-\u003ebuildFromBuilder(User::newQuery(),['email', 'name'], ['chunk' =\u003e 500]);\n~~~\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpkg6%2Fthink-csv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpkg6%2Fthink-csv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpkg6%2Fthink-csv/lists"}