Skip to content

Commit 9e19825

Browse files
committed
Add Laravel Excel and FastExcel quick integration docs.
1 parent 9e4c232 commit 9e19825

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

buttons-fast-excel.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Fast Excel Integration
2+
3+
[Fast-Excel](https://github.com/rap2hpoutre/fast-excel) is recommended when exporting bulk records.
4+
5+
## Usage
6+
7+
1. Install `fast-excel` using `composer require rap2hpoutre/fast-excel`.
8+
2. Create a dataTable class `php artisan datatables:make Users`
9+
3. Adjust `UsersDataTable` as needed.
10+
4. Set property `$fastExcel = true`.
11+
12+
```php
13+
class UsersDataTable extends DataTable
14+
{
15+
protected $fastExcel = true;
16+
17+
...
18+
}
19+
```
20+
21+
5. DataTables will now export csv & excel using `fast-excel` package.
22+
23+
24+
## Faster export by disabling the fast-excel callback
25+
26+
1. Just set property `$fastExcelCallback = false`. This is enabled by default for a better formatted output of exported file.
27+
28+
```php
29+
class UsersDataTable extends DataTable
30+
{
31+
protected $fastExcel = true;
32+
protected $fastExcelCallback = false;
33+
34+
```
35+
36+
2. Exported file will now be based on how your query was structured. No header formatting and all selected columns in sql will be included in the output.
37+
38+
## Using custom callback
39+
40+
Just override the `fastExcelCallback` method:
41+
42+
```php
43+
class UsersDataTable extends DataTable
44+
{
45+
protected $fastExcel = true;
46+
47+
public function fastExcelCallback()
48+
{
49+
return function ($row) {
50+
return [
51+
'Name' => $row['name'],
52+
'Email' => $row['email'],
53+
];
54+
};
55+
}
56+
57+
...
58+
```

buttons-laravel-excel.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Laravel Excel Integration
2+
3+
[Laravel Excel](https://github.com/Maatwebsite/Laravel-Excel) is the default package used when exporting DataTables to Excel and CSV.
4+
5+
## Using Export Class
6+
7+
1. Create an export class `php artisan make:export UsersExport`
8+
2. Update the generated export class and extend `DataTablesCollectionExport`
9+
10+
```php
11+
namespace App\Exports;
12+
13+
use Yajra\DataTables\Exports\DataTablesCollectionExport;
14+
15+
class UsersExport extends DataTablesCollectionExport
16+
{
17+
18+
}
19+
```
20+
21+
3. Update your `UsersDataTable` class and set `protected $exportClass = UsersExport::class`
22+
23+
```php
24+
class UsersDataTable extends DataTable
25+
{
26+
protected $exportClass = UsersExport::class;
27+
28+
```
29+
30+
4. Update your export class as needed. See official package docs: https://docs.laravel-excel.com/3.1/exports/collection.html
31+
32+
## Example Export Class
33+
34+
```php
35+
namespace App\Exports;
36+
37+
use Maatwebsite\Excel\Concerns\WithMapping;
38+
use Yajra\DataTables\Exports\DataTablesCollectionExport;
39+
40+
class UsersExport extends DataTablesCollectionExport implements WithMapping
41+
{
42+
public function headings(): array
43+
{
44+
return [
45+
'Name',
46+
'Email',
47+
];
48+
}
49+
50+
public function map($row): array
51+
{
52+
return [
53+
$row['name'],
54+
$row['email'],
55+
];
56+
}
57+
}
58+
```
59+

documentation.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@
103103
- [Sending Parameters](/docs/{{package}}/{{version}}/buttons-with)
104104
- [Extended DataTable](/docs/{{package}}/{{version}}/buttons-extended)
105105
- [Buttons Command](/docs/{{package}}/{{version}}/buttons-console)
106+
- [Laravel Excel Export](/docs/{{package}}/{{version}}/buttons-laravel-excel)
107+
- [Fast Excel Export](/docs/{{package}}/{{version}}/buttons-fast-excel)
106108
- [Github](https://github.com/yajra/laravel-datatables-buttons)
107109

108110
- ## Editor

0 commit comments

Comments
 (0)