From 43d0f4ca306fac2248bf9b2bea951c3430103a9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Alfaiate?= Date: Tue, 10 Jun 2025 11:04:26 +0700 Subject: [PATCH] fix: support for array notation --- src/Jobs/DataTableExportJob.php | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/Jobs/DataTableExportJob.php b/src/Jobs/DataTableExportJob.php index dcab695..44b429c 100644 --- a/src/Jobs/DataTableExportJob.php +++ b/src/Jobs/DataTableExportJob.php @@ -9,7 +9,7 @@ use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldBeUnique; use Illuminate\Contracts\Queue\ShouldQueue; -use Illuminate\Contracts\Support\Arrayable; +use Illuminate\Database\Eloquent\Model; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Http\File; use Illuminate\Queue\InteractsWithQueue; @@ -125,12 +125,6 @@ public function handle(): void foreach ($query as $row) { $cells = []; - $row = $row instanceof Arrayable ? $row->toArray() : (array) $row; - - if ($this->usesLazyMethod() && is_array($row)) { - $row = Arr::dot($row); - } - $defaultDateFormat = 'yyyy-mm-dd'; if (config('datatables-export.default_date_format') && is_string(config('datatables-export.default_date_format')) @@ -147,7 +141,7 @@ public function handle(): void } /** @var array|bool|int|string|null|DateTimeInterface $value */ - $value = $row[$property] ?? ''; + $value = $this->getValue($row, $property) ?? ''; if (isset($column->exportRender)) { $callback = $column->exportRender; @@ -231,6 +225,24 @@ protected function getExportableColumns(DataTable $dataTable): Collection return $columns->filter(fn (Column $column) => $column->exportable); } + protected function getValue(array|Model $row, string $property): mixed + { + [$currentProperty, $glue, $childProperty] = array_pad(preg_split('/\[(.*?)\]\.?/', $property, 2, PREG_SPLIT_DELIM_CAPTURE), 3, null); + + if ($glue === '') { + $glue = ', '; + } + + /** @var string $currentProperty */ + $value = data_get($row, $currentProperty.($childProperty ? '.*' : '')); + + if ($childProperty) { + $value = Arr::map($value, fn ($v) => $this->getValue($v, $childProperty)); + } + + return $glue ? Arr::join($value, $glue) : $value; + } + protected function usesLazyMethod(): bool { return config('datatables-export.method', 'lazy') === 'lazy';