File tree Expand file tree Collapse file tree 3 files changed +59
-44
lines changed
Expand file tree Collapse file tree 3 files changed +59
-44
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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 /**
Original file line number Diff line number Diff line change 22
33namespace 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}
You can’t perform that action at this time.
0 commit comments