From 9165ca662e88c03ddece0f933d5144ebf5d2c819 Mon Sep 17 00:00:00 2001 From: hellokfk <44089201+hellokfk@users.noreply.github.com> Date: Mon, 21 Feb 2022 14:59:29 +0300 Subject: [PATCH] Check if attribute relation is loaded when resolving attribute value (#109) --- src/NovaBelongsToDepend.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/NovaBelongsToDepend.php b/src/NovaBelongsToDepend.php index 0b413df..0514246 100644 --- a/src/NovaBelongsToDepend.php +++ b/src/NovaBelongsToDepend.php @@ -135,7 +135,12 @@ public function resolve($resource, $attribute = null) $foreign = $resource->{$this->attribute}(); $this->foreignKeyName = $foreign->getForeignKeyName(); - $value = $resource->{$this->attribute}()->withoutGlobalScopes()->first(); + if ($resource->relationLoaded($this->attribute)) { + $value = $resource->getRelation($this->attribute); + } else { + $value = $resource->{$this->attribute}()->withoutGlobalScopes()->first(); + } + if ($value) { $this->valueKey = $value->getKey(); $this->value = $this->formatDisplayValue($value);