sukohi/eloquent-export
Composer 安装命令:
composer require sukohi/eloquent-export
包简介
A Laravel package that allows you to export data from database through Eloquent model.
README 文档
README
A Laravel package that allows you to export data from your database through Eloquent model. This package is maintained under Laravel 6.x.
Installation
Execute the following command.
composer require sukohi/eloquent-export:2.*
Your Eloquent model already has export() method after installation.
Usage
The simplest way
Call export() in your controller.
Only filename required.
$users = \App\User::get();
// csv
return $users->export('test.csv');
// xlsx
return $users->export('test.xlsx');
// xls
return $users->export('test.xls');
Select column
Downloading data will be narrowed with select() method.
$users = \App\User::select('id', 'name', 'email')->get();
$users->export('test.csv); // Only id, name and email data.
Options
Rendering
You can change downloading data with key, dot-notation-key, null, callback(s) as follows.
$users = \App\User::get();
$options = [
'render' => [
'id', // $user->id
'company.name', // $user->company->name
null, // blank column
function($user) {
return $user->created_at->format('Y.m.d');
}
]
];
return $users->export('test.xls', $options);
Encoding
You can specify an encoding to convert data just when downloading csv file.
$options = ['encoding' => 'sjis-win'];
return $users->export('test.csv', $options);
Additional data
$users = \App\User::get();
$options = [
'prepend' => [
['header - 1', 'header - 2', 'header - 3']
],
'append' => [
['footer - 1', 'footer - 2', 'footer - 3']
]
];
return $users->export('test.xls', $options);
License
This package is licensed under the MIT License.
Copyright 2019 Sukohi Kuhoh
统计信息
- 总下载量: 588
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-11-21