From 69a23f701667ff2bbd0d918588dce3178090cfe9 Mon Sep 17 00:00:00 2001 From: Cameron Wilby Date: Mon, 12 Apr 2021 01:09:30 -0700 Subject: [PATCH] Check if attribute relation is loaded when resolving attribute value (#86) --- src/NovaBelongsToDepend.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/NovaBelongsToDepend.php b/src/NovaBelongsToDepend.php index 9a75ea6..80d5e70 100644 --- a/src/NovaBelongsToDepend.php +++ b/src/NovaBelongsToDepend.php @@ -135,8 +135,15 @@ public function resolve($resource, $attribute = null) $this->dependKey = $resource->{$this->dependsOn}()->getForeignKeyName(); } - $value = $resource->{$this->attribute}()->withoutGlobalScopes()->first(); - if ($value) { + if ($resource->relationLoaded($this->attribute)) { + $value = $resource->getRelation($this->attribute); + } + + if (empty($value)) { + $value = $resource->{$this->attribute}()->withoutGlobalScopes()->getResults(); + } + + if (!empty($value)) { $this->valueKey = $value->getKey(); $this->value = $this->formatDisplayValue($value); }