diff --git a/src/Actions/ExportToExcel.php b/src/Actions/ExportToExcel.php index fde8e60..7668ae5 100644 --- a/src/Actions/ExportToExcel.php +++ b/src/Actions/ExportToExcel.php @@ -193,7 +193,7 @@ public function map($row): array $row->setHidden([]); $row = $this->replaceFieldValuesWhenOnResource( $row, - is_array($only) ? $only : array_keys($row->attributesToArray()) + $only ); } diff --git a/src/Concerns/Only.php b/src/Concerns/Only.php index 4ffd260..985acfd 100644 --- a/src/Concerns/Only.php +++ b/src/Concerns/Only.php @@ -8,9 +8,9 @@ trait Only { /** - * @var array|null + * @var array */ - protected $only; + protected $only = []; /** * @var bool @@ -50,7 +50,7 @@ public function allFields() } /** - * @return array|null + * @return array */ public function getOnly() { @@ -64,8 +64,14 @@ protected function handleOnly(ExportActionRequest $request) { // If not a specific only array, and user wants only index fields, // fill the only with the index fields. - if ($this->onlyIndexFields && (!is_array($this->only) || count($this->only) === 0)) { - $this->only = $request->indexFields($request->newResource()); + $indexFields = $request->indexFields($request->newResource()); + + if ($this->onlyIndexFields && count($this->only) === 0) { + $this->only = $indexFields; + } elseif (!$this->onlyIndexFields) { + $modelAttributes = optional($request->toQuery()->first())->attributesToArray() ?: []; + + $this->only = array_merge(array_keys($modelAttributes), $indexFields); } } }