wilber/wilbercsv
Composer 安装命令:
composer require wilber/wilbercsv
包简介
A Laravel package to easily generate CSV files from Eloquent model.
README 文档
README
A Laravel package to easily generate CSV files from Eloquent model.
Basic usage
$users = User::get(); // All users $csvExporter = new \Laracsv\Export(); $csvExporter->build($users, ['email', 'name'])->download();
And a proper CSV file will be downloaded with email and name fields.
Installation
Just run this on your terminal:
composer require "wilber/wilbercsv"
and you should be good to go.
Full Documentation
Build CSV
$exporter->build($modelCollection, $fields) takes two parameters.
First one is the model (collection of models), and seconds one takes the field names
you want to export.
$csvExporter->build(User::get(), ['email', 'name', 'created_at']);
Output Options
Download
To get file downloaded to the browser:
$csvExporter->download();
You can provide a filename if you wish:
$csvExporter->download('active_users.csv');
If no filename is given a filename with date-time will be generated.
Advanced Outputs
LaraCSV uses League CSV. You can do what League CSV is able to do. You can get the underlying League CSV instance by calling:
$csv = $csvExporter->getCsv();
And then you can do several things like:
$csv->toHTML(); // To output the CSV as an HTML table $csv->jsonSerialize()(); // To turn the CSV in to an array $csv = (string) $csv; // To get the CSV as string echo $csv; // To print the CSV
For more information please check League CSV documentation.
Custom Headers
Above code example will generate a CSV with headers email, name, created_at and corresponding rows after.
If you want to change the header with a custom label just pass it as array value:
$csvExporter->build(User::get(), ['email', 'name' => 'Full Name', 'created_at' => 'Joined']);
Now name column will show the header Full Name but it will still take
values from name field of the model.
Modify or Add Values
There is a hook which is triggered before processing a database row. For example, if you want to change the date format you can do so.
$csvExporter = new \Laracsv\Export(); $users = User::get(); // Register the hook before building $csvExporter->beforeEach(function ($user) { $user->created_at = date('f', strtotime($user->created_at)); }); $csvExporter->build($users, ['email', 'name' => 'Full Name', 'created_at' => 'Joined']);
Note: If a beforeEach callback returns false then the entire will be
excluded from the CSV. It can come handy to filter some rows.
Add fields and values
You may also add fields that don't exists in a database table add values on the fly:
// The notes field doesn't exist so values for this field will be blank by default $csvExporter->beforeEach(function ($user) { // Now notes field will have this value $user->notes = 'Add your notes'; }); $csvExporter->build($users, ['email', 'notes']);
Model Relationships
You can also add fields in the CSV from related database tables, given the model has relationships defined.
This will get the product title and the related category's title (one to one):
$csvExporter->build($products, ['title', 'category.title']);
You may also tinker relation things as you wish with hooks:
$products = Product::where('order_count', '>', 10)->orderBy('order_count', 'desc')->get(); $fields = ['id', 'title','original_price' => 'Market Price', 'category_ids',]; $csvExporter = new \Laracsv\Export(); $csvExporter->beforeEach(function ($product) { $product->category_ids = implode(', ', $product->categories->pluck('id')->toArray()); });
Road Map
- Import CSV
wilber/wilbercsv 适用场景与选型建议
wilber/wilbercsv 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.79k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 06 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「csv」 「export」 「laravel」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 wilber/wilbercsv 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wilber/wilbercsv 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 wilber/wilbercsv 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Bulk export of sylius resources
Yii2 export extension
laravel facade to read/write csv file
A simple API extension for SplFileObject
Collection of tools to use the full power of the Enyalius framework
Export any WordPress post type to CVS.
统计信息
- 总下载量: 2.79k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-06-15