10
10
use Illuminate \Contracts \Queue \ShouldBeUnique ;
11
11
use Illuminate \Contracts \Queue \ShouldQueue ;
12
12
use Illuminate \Contracts \Support \Arrayable ;
13
+ use Illuminate \Database \Eloquent \Builder ;
14
+ use Illuminate \Database \Eloquent \Model ;
13
15
use Illuminate \Foundation \Bus \Dispatchable ;
14
16
use Illuminate \Http \File ;
15
17
use Illuminate \Queue \InteractsWithQueue ;
@@ -125,12 +127,6 @@ public function handle(): void
125
127
foreach ($ query as $ row ) {
126
128
$ cells = [];
127
129
128
- $ row = $ row instanceof Arrayable ? $ row ->toArray () : (array ) $ row ;
129
-
130
- if ($ this ->usesLazyMethod () && is_array ($ row )) {
131
- $ row = Arr::dot ($ row );
132
- }
133
-
134
130
$ defaultDateFormat = 'yyyy-mm-dd ' ;
135
131
if (config ('datatables-export.default_date_format ' )
136
132
&& is_string (config ('datatables-export.default_date_format ' ))
@@ -147,7 +143,7 @@ public function handle(): void
147
143
}
148
144
149
145
/** @var array|bool|int|string|null|DateTimeInterface $value */
150
- $ value = $ row[ $ property] ?? '' ;
146
+ $ value = $ this -> getValue ( $ row, $ property) ?? '' ;
151
147
152
148
if (isset ($ column ->exportRender )) {
153
149
$ callback = $ column ->exportRender ;
@@ -231,6 +227,23 @@ protected function getExportableColumns(DataTable $dataTable): Collection
231
227
return $ columns ->filter (fn (Column $ column ) => $ column ->exportable );
232
228
}
233
229
230
+ protected function getValue (array |Model $ row , string $ property ): mixed
231
+ {
232
+ [$ currentProperty , $ glue , $ childProperty ] = array_pad (preg_split ('/\[(.*?)\]\.?/ ' , $ property , 2 , PREG_SPLIT_DELIM_CAPTURE ), 3 , null );
233
+
234
+ if ($ glue === '' ) {
235
+ $ glue = ', ' ;
236
+ }
237
+
238
+ $ value = data_get ($ row , $ currentProperty .($ childProperty ? '.* ' : '' ));
239
+
240
+ if ($ childProperty ) {
241
+ $ value = Arr::map ($ value , fn ($ v ) => $ this ->getValue ($ v , $ childProperty ));
242
+ }
243
+
244
+ return $ glue ? Arr::join ($ value , $ glue ) : $ value ;
245
+ }
246
+
234
247
protected function usesLazyMethod (): bool
235
248
{
236
249
return config ('datatables-export.method ' , 'lazy ' ) === 'lazy ' ;
0 commit comments