8
8
use Maatwebsite \Excel \Concerns \WithColumnFormatting ;
9
9
use Maatwebsite \Excel \Concerns \WithHeadings ;
10
10
use Maatwebsite \Excel \Concerns \WithMapping ;
11
+ use PhpOffice \PhpSpreadsheet \Shared \Date ;
11
12
use PhpOffice \PhpSpreadsheet \Style \NumberFormat ;
12
13
use Yajra \DataTables \Html \Column ;
13
14
@@ -18,6 +19,13 @@ class DataTableQueuedExport implements FromQuery, WithMapping, WithHeadings, Wit
18
19
protected $ query ;
19
20
protected $ columns ;
20
21
22
+ /**
23
+ * Index of fields with date instance.
24
+ *
25
+ * @var array
26
+ */
27
+ protected $ dates = [];
28
+
21
29
public function __construct ($ query , Collection $ columns )
22
30
{
23
31
$ this ->query = $ query ;
@@ -32,8 +40,16 @@ public function query()
32
40
public function map ($ row ): array
33
41
{
34
42
return $ this ->columns
35
- ->map (function (Column $ column ) use ($ row ) {
36
- return $ row [$ column ['data ' ]];
43
+ ->map (function (Column $ column , $ index ) use ($ row ) {
44
+ $ property = $ column ['data ' ];
45
+
46
+ if ($ row [$ property ] instanceof \DateTime) {
47
+ $ this ->dates [] = $ index ;
48
+
49
+ return Date::dateTimeToExcel ($ row [$ property ]);
50
+ }
51
+
52
+ return $ row [$ property ];
37
53
})
38
54
->toArray ();
39
55
}
@@ -49,7 +65,13 @@ public function columnFormats(): array
49
65
50
66
$ this ->columns
51
67
->each (function (Column $ column , $ index ) use (&$ formats ) {
52
- $ formats [$ this ->num2alpha ($ index - 1 )] = $ column ['exportFormat ' ] ?? NumberFormat::FORMAT_TEXT ;
68
+ if (in_array ($ index , $ this ->dates )) {
69
+ return $ formats [$ this ->num2alpha ($ index - 1 )] = NumberFormat::FORMAT_DATE_YYYYMMDD ;
70
+ }
71
+
72
+ if (isset ($ column ['exportFormat ' ])) {
73
+ return $ formats [$ this ->num2alpha ($ index - 1 )] = $ column ['exportFormat ' ];
74
+ }
53
75
})
54
76
->toArray ();
55
77
0 commit comments