diff --git a/src/Domain/Relation/RelationHandlerResolver.php b/src/Domain/Relation/RelationHandlerResolver.php index 11bd27d..25ffcb6 100644 --- a/src/Domain/Relation/RelationHandlerResolver.php +++ b/src/Domain/Relation/RelationHandlerResolver.php @@ -4,6 +4,7 @@ use DomainException; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\App; use PhoenixLib\NovaNestedTreeAttachMany\Domain\Relation\Handlers\RelationHandler; class RelationHandlerResolver implements RelationHandlerFactory @@ -16,9 +17,14 @@ public function __construct(){ public function make( $relation ): RelationHandler { - if($this->handlers->has( $relation )) + $handler = $this->handlers->first(function (RelationHandler $handler) use ($relation){ + $handlerRelation = $handler->relation(); + return $relation instanceof $handlerRelation; + }); + + if($handler) { - return $this->handlers->get( $relation ); + return $handler; } throw new DomainException(sprintf('RelationHandler for relation: %s is not registered', $relation)); diff --git a/src/Http/Controllers/NestedTreeController.php b/src/Http/Controllers/NestedTreeController.php index f7a0ec4..5f98b41 100644 --- a/src/Http/Controllers/NestedTreeController.php +++ b/src/Http/Controllers/NestedTreeController.php @@ -19,9 +19,7 @@ public function attached(NovaRequest $request, $resource, $resourceId, $relation { $model = $request->findResourceOrFail()->model(); - $relation = get_class($model->{$relationship}()); - - $handler = $this->factory->make($relation); + $handler = $this->factory->make($model->{$relationship}()); return $handler->retrieve($model, $relationship, $idKey); } diff --git a/src/NestedTreeAttachManyField.php b/src/NestedTreeAttachManyField.php index 5c28fee..2f9441c 100644 --- a/src/NestedTreeAttachManyField.php +++ b/src/NestedTreeAttachManyField.php @@ -41,9 +41,7 @@ public function __construct($name, $attribute = null, $resource = null) $factory = App::make(RelationHandlerFactory::class); - $relation = get_class($model->{$attribute}()); - - $handler = $factory->make($relation); + $handler = $factory->make($model->{$attribute}()); $handler->attach($model, $attribute, json_decode($request->{$attribute}, true));