From 59fa6a5c6e5959d7efb6edd5e5c348b45357520b Mon Sep 17 00:00:00 2001 From: LT Date: Sat, 30 Nov 2024 15:59:53 +0300 Subject: [PATCH] fix: HasMany --- .../src/Fields/Relationships/HasMany.php | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/Laravel/src/Fields/Relationships/HasMany.php b/src/Laravel/src/Fields/Relationships/HasMany.php index 8ab125003..5c0640774 100644 --- a/src/Laravel/src/Fields/Relationships/HasMany.php +++ b/src/Laravel/src/Fields/Relationships/HasMany.php @@ -346,19 +346,23 @@ protected function getTablePreview(): TableBuilderContract $resource = clone $this->getResource() ->disableSaveQueryState(); - $resource->disableQueryFeatures(); + if(\is_null($this->modifyBuilder) && $this->getRelatedModel()?->relationLoaded($this->getRelationName()) === true) { + $items = $this->toRelatedCollection(); + } else { + $resource->disableQueryFeatures(); - $casted = $this->getRelatedModel(); - $relation = $casted?->{$this->getRelationName()}(); + $casted = $this->getRelatedModel(); + $relation = $casted?->{$this->getRelationName()}(); - /** @var Builder $query */ - $query = \is_null($this->modifyBuilder) - ? $relation - : value($this->modifyBuilder, $relation); + /** @var Builder $query */ + $query = \is_null($this->modifyBuilder) + ? $relation + : value($this->modifyBuilder, $relation); - $resource->customQueryBuilder($query->limit($this->getLimit())); + $resource->customQueryBuilder($query->limit($this->getLimit())); - $items = $resource->getQuery()->get(); + $items = $resource->getQuery()->get(); + } return TableBuilder::make(items: $items) ->fields($this->getFieldsOnPreview())