Skip to content

Commit d8b7109

Browse files
authored
Merge pull request #122 from yajra/better-export
[4.x] Improve support for custom laravel-excel export class using collection.
2 parents b1311d0 + fabb4be commit d8b7109

File tree

3 files changed

+59
-44
lines changed

3 files changed

+59
-44
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Yajra\DataTables\Exports;
4+
5+
use Illuminate\Support\Collection;
6+
use Maatwebsite\Excel\Concerns\Exportable;
7+
use Maatwebsite\Excel\Concerns\FromCollection;
8+
use Maatwebsite\Excel\Concerns\WithHeadings;
9+
10+
abstract class DataTablesCollectionExport implements FromCollection, WithHeadings
11+
{
12+
use Exportable;
13+
14+
/**
15+
* @var Collection
16+
*/
17+
protected $collection;
18+
19+
/**
20+
* @param Collection $collection
21+
*/
22+
public function __construct(Collection $collection)
23+
{
24+
$this->collection = $collection;
25+
}
26+
27+
/**
28+
* @return Collection
29+
*/
30+
public function collection()
31+
{
32+
return $this->collection;
33+
}
34+
35+
/**
36+
* @return array
37+
*/
38+
public function headings(): array
39+
{
40+
$first = $this->collection->first();
41+
if ($first) {
42+
return array_keys($first);
43+
}
44+
45+
return [];
46+
}
47+
}

src/Services/DataTable.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,9 +405,17 @@ public function excel()
405405
*/
406406
protected function buildExcelFile()
407407
{
408-
$dataForExport = collect($this->getDataForExport());
408+
if ($this->exportClass != DataTablesExportHandler::class) {
409+
$collection = collect($this->getAjaxResponseData());
410+
} else {
411+
$collection = collect($this->getDataForExport());
412+
}
413+
414+
if (method_exists($collection, 'lazy')) {
415+
$collection->lazy();
416+
}
409417

410-
return new $this->exportClass($dataForExport);
418+
return new $this->exportClass($collection);
411419
}
412420

413421
/**

src/Services/DataTablesExportHandler.php

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,8 @@
22

33
namespace Yajra\DataTables\Services;
44

5-
use Illuminate\Support\Collection;
6-
use Maatwebsite\Excel\Concerns\Exportable;
7-
use Maatwebsite\Excel\Concerns\FromCollection;
8-
use Maatwebsite\Excel\Concerns\WithHeadings;
5+
use Yajra\DataTables\Exports\DataTablesCollectionExport;
96

10-
class DataTablesExportHandler implements FromCollection, WithHeadings
7+
class DataTablesExportHandler extends DataTablesCollectionExport
118
{
12-
use Exportable;
13-
14-
/**
15-
* @var Collection
16-
*/
17-
protected $collection;
18-
19-
/**
20-
* DataTablesExportHandler constructor.
21-
*
22-
* @param Collection $collection
23-
*/
24-
public function __construct(Collection $collection)
25-
{
26-
$this->collection = $collection;
27-
}
28-
29-
/**
30-
* @return Collection
31-
*/
32-
public function collection()
33-
{
34-
return $this->collection;
35-
}
36-
37-
/**
38-
* @return array
39-
*/
40-
public function headings(): array
41-
{
42-
$first = $this->collection->first();
43-
if ($first) {
44-
return array_keys($first);
45-
}
46-
47-
return [];
48-
}
499
}

0 commit comments

Comments
 (0)