From 9aa97d6464921a2a714b695a7850d4a96c491ed5 Mon Sep 17 00:00:00 2001 From: Arthur Kirkosa Date: Fri, 20 Jan 2023 16:56:42 +0200 Subject: [PATCH] Improve flow for resolveIndexAttributes (#532) --- src/Repositories/Repository.php | 39 ++++++++++++++++----------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/src/Repositories/Repository.php b/src/Repositories/Repository.php index 7ca0b9a1..afd1095e 100644 --- a/src/Repositories/Repository.php +++ b/src/Repositories/Repository.php @@ -436,28 +436,11 @@ public function resolveShowAttributes(RestifyRequest $request) */ public function resolveIndexAttributes($request) { - // Resolve the show method, and attach the value to the array - $fields = $this - ->collectFields($request) - ->when($this->hasCustomColumns(), fn (FieldCollection $fields) => $fields->inList($this->getColumns())) - ->forIndex($request, $this) - ->filter(fn (Field $field) => $field->authorize($request)) - ->when( - $this->eagerState, - function ($items) { - return $items->filter(fn (Field $field) => ! $field instanceof EagerField); - } - ) - ->each(fn (Field $field) => $field->resolveForIndex($this)) - ->map(fn (Field $field) => $field->serializeToValue($request)) - ->mapWithKeys(fn ($value) => $value) - ->all(); - if ($this instanceof Mergeable) { // Hidden and authorized index fields - $fields = $this->modelAttributes($request) + return $this->modelAttributes($request) ->filter(function ($value, $attribute) use ($request) { - /** * @var Field $field */ + /** @var Field $field */ $field = $this->collectFields($request)->firstWhere('attribute', $attribute); if (is_null($field)) { @@ -476,7 +459,23 @@ function ($items) { })->all(); } - return $fields; + // Resolve the show method, and attach the value to the array + return $this + ->collectFields($request) + ->when( + $this->hasCustomColumns(), + fn (FieldCollection $fields) => $fields->inList($this->getColumns()) + ) + ->forIndex($request, $this) + ->filter(fn (Field $field) => $field->authorize($request)) + ->when( + $this->eagerState, + fn ($items) => $items->filter(fn (Field $field) => ! $field instanceof EagerField) + ) + ->each(fn (Field $field) => $field->resolveForIndex($this)) + ->map(fn (Field $field) => $field->serializeToValue($request)) + ->mapWithKeys(fn ($value) => $value) + ->all(); } public function resolveShowMeta($request)