Skip to content

Commit

Permalink
fix -- support for custom relationships that extends base BelongsTo, …
Browse files Browse the repository at this point in the history
…BelongsToMany, HasMany relationships
  • Loading branch information
phoenix committed Jan 15, 2021
1 parent c8464b9 commit 7b723be
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
10 changes: 8 additions & 2 deletions src/Domain/Relation/RelationHandlerResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));
Expand Down
4 changes: 1 addition & 3 deletions src/Http/Controllers/NestedTreeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 1 addition & 3 deletions src/NestedTreeAttachManyField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down

0 comments on commit 7b723be

Please sign in to comment.