From 0bece955a2f88e60c88f503a33ebf6000960380d Mon Sep 17 00:00:00 2001 From: Gautier DELEGLISE Date: Fri, 7 May 2021 17:27:56 +0200 Subject: [PATCH 1/4] :sparkles: added possibility to modify export fields and alterate query --- src/Actions/ExportToExcel.php | 87 +++++++++++++++++++++++++++-------- 1 file changed, 67 insertions(+), 20 deletions(-) diff --git a/src/Actions/ExportToExcel.php b/src/Actions/ExportToExcel.php index 7668ae5..ba6c0fc 100644 --- a/src/Actions/ExportToExcel.php +++ b/src/Actions/ExportToExcel.php @@ -51,6 +51,16 @@ class ExportToExcel extends Action implements FromQuery, WithCustomChunkSize, Wi */ protected $resource; + /** + * @var callable|null + */ + protected $alterateQuery; + + /** + * @var array|null + */ + protected $exportFields; + /** * @var Builder */ @@ -90,6 +100,10 @@ public function handleRequest(ActionRequest $request) $this->handleOnly($this->request); $this->handleHeadings($query, $this->request); + if (!is_null($this->alterateQuery)) { + ($this->alterateQuery)($query); + } + return $this->handle($request, $this->withQuery($query)); } @@ -119,6 +133,30 @@ public function handle(ActionRequest $request, Action $exportable): array : Action::message(__('Resource was successfully exported.')); } + /** + * @param callable $callable + * + * @return $this + */ + public function alterateQuery(callable $callable) + { + $this->alterate = $callable; + + return $this; + } + + /** + * @param array $fields + * + * @return $this + */ + public function exportFields(array $fields) + { + $this->exportFields = $fields; + + return $this; + } + /** * @param callable $callback * @@ -237,33 +275,42 @@ protected function getDefaultExtension(): string protected function replaceFieldValuesWhenOnResource(Model $model, array $only = []): array { $resource = $this->resolveResource($model); - $fields = $this->resourceFields($resource); + $fields = $this->exportFields ?? $this->resourceFields($resource); $row = []; - foreach ($fields as $field) { - if (!$this->isExportableField($field)) { - continue; - } - if (\in_array($field->attribute, $only, true)) { - $row[$field->attribute] = $field->value; - } elseif (\in_array($field->name, $only, true)) { - // When no field could be found by their attribute name, it's most likely a computed field. - $row[$field->name] = $field->value; + // If custom user fields + if (is_array($fields)) { + foreach ($fields as $field) { + $row[$field] = $model->{$field}; + } + } else { + // If index fields + foreach ($fields as $field) { + if (!$this->isExportableField($field)) { + continue; + } + + if (\in_array($field->attribute, $only, true)) { + $row[$field->attribute] = $field->value; + } elseif (\in_array($field->name, $only, true)) { + // When no field could be found by their attribute name, it's most likely a computed field. + $row[$field->name] = $field->value; + } } - } - // Add fields that were requested by ->only(), but are not registered as fields in the Nova resource. - foreach (array_diff($only, array_keys($row)) as $attribute) { - if ($model->{$attribute}) { - $row[$attribute] = $model->{$attribute}; - } else { - $row[$attribute] = ''; + // Add fields that were requested by ->only(), but are not registered as fields in the Nova resource. + foreach (array_diff($only, array_keys($row)) as $attribute) { + if ($model->{$attribute}) { + $row[$attribute] = $model->{$attribute}; + } else { + $row[$attribute] = ''; + } } - } - // Fix sorting - $row = array_merge(array_flip($only), $row); + // Fix sorting + $row = array_merge(array_flip($only), $row); + } return $row; } From 71b912b0a97717ab80230ecdcc1fc2d7c742ec56 Mon Sep 17 00:00:00 2001 From: Gautier DELEGLISE Date: Fri, 7 May 2021 17:32:07 +0200 Subject: [PATCH 2/4] :memo: updated README --- README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/README.md b/README.md index 875fc6b..1eda69e 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,32 @@ class User extends Resource More installation instructions can be found at: [https://docs.laravel-excel.com/nova/1.1/getting-started/installation.html](https://docs.laravel-excel.com/nova/1.1/getting-started/installation.html) +## Modify your export + +Currently the export is based on the index fields but this might not be what you want to do. +You can modify with ease the query and the exported fields this way: + +```php + /** + * Get the actions available for the resource. + * + * @param \Illuminate\Http\Request $request + * @return array + */ + public function actions(Request $request) + { + return [ + (new DownloadExcel) + ->alterateQuery(function ($query) { + $query->select('id', 'first_name'); + }) + ->exportFields(['first_name', 'id']), + ]; + } +``` + +Feel free to join tables if necessary but you should always select the fields you want to export to avoid confusions. + ## 🎓 Learning Laravel Excel You can find the full documentation of Laravel Nova Excel [on the website](https://docs.laravel-excel.com/nova). From b6ba506a9b3edb9a1c85566ba196b628a2fce8d2 Mon Sep 17 00:00:00 2001 From: Gautier Date: Thu, 19 Aug 2021 22:33:51 +0200 Subject: [PATCH 3/4] :recycle: refactored code --- src/Actions/ExportToExcel.php | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/Actions/ExportToExcel.php b/src/Actions/ExportToExcel.php index ba6c0fc..2c6fd7e 100644 --- a/src/Actions/ExportToExcel.php +++ b/src/Actions/ExportToExcel.php @@ -54,12 +54,12 @@ class ExportToExcel extends Action implements FromQuery, WithCustomChunkSize, Wi /** * @var callable|null */ - protected $alterateQuery; + protected $callbackQuery; /** * @var array|null */ - protected $exportFields; + protected $exportAttributes; /** * @var Builder @@ -100,8 +100,8 @@ public function handleRequest(ActionRequest $request) $this->handleOnly($this->request); $this->handleHeadings($query, $this->request); - if (!is_null($this->alterateQuery)) { - ($this->alterateQuery)($query); + if (is_callable($this->callbackQuery)) { + ($this->callbackQuery)($query); } return $this->handle($request, $this->withQuery($query)); @@ -138,21 +138,22 @@ public function handle(ActionRequest $request, Action $exportable): array * * @return $this */ - public function alterateQuery(callable $callable) + public function tapQuery(callable $callable) { - $this->alterate = $callable; + $this->callbackQuery = $callable; return $this; } /** - * @param array $fields + * Sets the attributes to be selected * + * @param array|mixed $exportAttributes * @return $this */ - public function exportFields(array $fields) + public function exportAttributes($exportAttributes) { - $this->exportFields = $fields; + $this->exportAttributes = is_array($exportAttributes) ? $exportAttributes : func_get_args(); return $this; } @@ -275,7 +276,7 @@ protected function getDefaultExtension(): string protected function replaceFieldValuesWhenOnResource(Model $model, array $only = []): array { $resource = $this->resolveResource($model); - $fields = $this->exportFields ?? $this->resourceFields($resource); + $fields = $this->exportAttributes ?? $this->resourceFields($resource); $row = []; From 7723c7f9a2b985475956856396f6a78fafd1b5ce Mon Sep 17 00:00:00 2001 From: Gautier Date: Thu, 19 Aug 2021 22:36:30 +0200 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=9A=A8=20fixed=20style=20return?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Actions/ExportToExcel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Actions/ExportToExcel.php b/src/Actions/ExportToExcel.php index 2c6fd7e..d9808b3 100644 --- a/src/Actions/ExportToExcel.php +++ b/src/Actions/ExportToExcel.php @@ -146,7 +146,7 @@ public function tapQuery(callable $callable) } /** - * Sets the attributes to be selected + * Sets the attributes to be selected. * * @param array|mixed $exportAttributes * @return $this