Skip to content

Commit ae583fb

Browse files
committed
fix: support for array notation
1 parent d947b5b commit ae583fb

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/Jobs/DataTableExportJob.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Illuminate\Contracts\Queue\ShouldBeUnique;
1111
use Illuminate\Contracts\Queue\ShouldQueue;
1212
use Illuminate\Contracts\Support\Arrayable;
13+
use Illuminate\Database\Eloquent\Builder;
14+
use Illuminate\Database\Eloquent\Model;
1315
use Illuminate\Foundation\Bus\Dispatchable;
1416
use Illuminate\Http\File;
1517
use Illuminate\Queue\InteractsWithQueue;
@@ -125,12 +127,6 @@ public function handle(): void
125127
foreach ($query as $row) {
126128
$cells = [];
127129

128-
$row = $row instanceof Arrayable ? $row->toArray() : (array) $row;
129-
130-
if ($this->usesLazyMethod() && is_array($row)) {
131-
$row = Arr::dot($row);
132-
}
133-
134130
$defaultDateFormat = 'yyyy-mm-dd';
135131
if (config('datatables-export.default_date_format')
136132
&& is_string(config('datatables-export.default_date_format'))
@@ -147,7 +143,7 @@ public function handle(): void
147143
}
148144

149145
/** @var array|bool|int|string|null|DateTimeInterface $value */
150-
$value = $row[$property] ?? '';
146+
$value = $this->getValue($row, $property) ?? '';
151147

152148
if (isset($column->exportRender)) {
153149
$callback = $column->exportRender;
@@ -231,6 +227,23 @@ protected function getExportableColumns(DataTable $dataTable): Collection
231227
return $columns->filter(fn (Column $column) => $column->exportable);
232228
}
233229

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+
234247
protected function usesLazyMethod(): bool
235248
{
236249
return config('datatables-export.method', 'lazy') === 'lazy';

0 commit comments

Comments
 (0)