From 4e7d1f352eff32c35d72d2f1b936e802726f0a1e Mon Sep 17 00:00:00 2001 From: Aleksei Zarubin Date: Sat, 10 Jul 2021 11:53:09 +0500 Subject: [PATCH] fix: fetching relation entity with custom key --- .../HandlesRelationOneToManyOperations.php | 5 ++++- ...HandlesRelationStandardBatchOperations.php | 22 ++++++++++++++----- .../HandlesRelationStandardOperations.php | 22 ++++++++++++++----- 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/src/Concerns/HandlesRelationOneToManyOperations.php b/src/Concerns/HandlesRelationOneToManyOperations.php index 39f1fe24..bd096f3e 100644 --- a/src/Concerns/HandlesRelationOneToManyOperations.php +++ b/src/Concerns/HandlesRelationOneToManyOperations.php @@ -170,7 +170,10 @@ public function dissociate(Request $request, $parentKey, $relatedKey) $this->performDissociate($request, $parentEntity, $entity); $entity = $this->relationQueryBuilder->buildQuery($entity::query(), $request) - ->with($this->relationsResolver->requestedRelations($request))->first(); + ->with($this->relationsResolver->requestedRelations($request))->where( + $this->resolveQualifiedKeyName(), + $entity->{$this->keyName()} + )->firstOrFail(); $afterHookResult = $this->afterDissociate($request, $parentEntity,$entity); if ($this->hookResponds($afterHookResult)) { diff --git a/src/Concerns/HandlesRelationStandardBatchOperations.php b/src/Concerns/HandlesRelationStandardBatchOperations.php index 1176f67e..00da6bdb 100644 --- a/src/Concerns/HandlesRelationStandardBatchOperations.php +++ b/src/Concerns/HandlesRelationStandardBatchOperations.php @@ -54,7 +54,10 @@ public function batchStore(Request $request, $parentKey) Arr::get($resource, 'pivot', []) ); - $entity = $this->newRelationQuery($parentEntity)->with($requestedRelations)->find($entity->id); + $entity = $this->newRelationQuery($parentEntity)->with($requestedRelations)->where( + $this->resolveQualifiedKeyName(), + $entity->{$this->keyName()} + )->first(); $entity->wasRecentlyCreated = true; $entity = $this->cleanupEntity($entity); @@ -168,7 +171,10 @@ public function batchUpdate(Request $request, $parentKey) Arr::get($resource, 'pivot', []) ); - $entity = $this->newRelationQuery($parentEntity)->with($requestedRelations)->find($entity->id); + $entity = $this->newRelationQuery($parentEntity)->with($requestedRelations)->where( + $this->resolveQualifiedKeyName(), + $entity->{$this->keyName()} + )->first(); $entity = $this->cleanupEntity($entity); @@ -336,9 +342,10 @@ public function batchDestroy(Request $request, $parentKey) if (!$forceDeletes) { $this->performDestroy($entity); if ($softDeletes) { - $entity = $this->newRelationQuery($parentEntity)->withTrashed()->with($requestedRelations)->find( - $entity->id - ); + $entity = $this->newRelationQuery($parentEntity)->withTrashed()->with($requestedRelations)->where( + $this->resolveQualifiedKeyName(), + $entity->{$this->keyName()} + )->firstOrFail(); } } else { $this->performForceDestroy($entity); @@ -481,7 +488,10 @@ public function batchRestore(Request $request, $parentKey) $this->performRestore($entity); - $entity = $this->newRelationQuery($parentEntity)->with($requestedRelations)->find($entity->id); + $entity = $this->newRelationQuery($parentEntity)->with($requestedRelations)->where( + $this->resolveQualifiedKeyName(), + $entity->{$this->keyName()} + )->firstOrFail(); $entity = $this->cleanupEntity($entity); diff --git a/src/Concerns/HandlesRelationStandardOperations.php b/src/Concerns/HandlesRelationStandardOperations.php index 1c568407..f21d2327 100644 --- a/src/Concerns/HandlesRelationStandardOperations.php +++ b/src/Concerns/HandlesRelationStandardOperations.php @@ -261,7 +261,10 @@ public function store(Request $request, $parentKey) $request->get('pivot', []) ); - $entity = $this->newRelationQuery($parentEntity)->with($requestedRelations)->find($entity->id); + $entity = $this->newRelationQuery($parentEntity)->with($requestedRelations)->where( + $this->resolveQualifiedKeyName(), + $entity->{$this->keyName()} + )->firstOrFail(); $entity->wasRecentlyCreated = true; $entity = $this->cleanupEntity($entity); @@ -590,7 +593,10 @@ public function update(Request $request, $parentKey, $relatedKey = null) $request->get('pivot', []) ); - $entity = $this->newRelationQuery($parentEntity)->with($requestedRelations)->find($entity->id); + $entity = $this->newRelationQuery($parentEntity)->with($requestedRelations)->where( + $this->resolveQualifiedKeyName(), + $entity->{$this->keyName()} + )->firstOrFail(); $entity = $this->cleanupEntity($entity); @@ -754,9 +760,10 @@ public function destroy(Request $request, $parentKey, $relatedKey = null) if (!$forceDeletes) { $this->performDestroy($entity); if ($softDeletes) { - $entity = $this->newRelationQuery($parentEntity)->withTrashed()->with($requestedRelations)->find( - $entity->id - ); + $entity = $this->newRelationQuery($parentEntity)->withTrashed()->with($requestedRelations)->where( + $this->resolveQualifiedKeyName(), + $entity->{$this->keyName()} + )->firstOrFail(); } } else { $this->performForceDestroy($entity); @@ -915,7 +922,10 @@ public function restore(Request $request, $parentKey, $relatedKey = null) $this->performRestore($entity); - $entity = $this->newRelationQuery($parentEntity)->with($requestedRelations)->find($entity->id); + $entity = $this->newRelationQuery($parentEntity)->with($requestedRelations)->where( + $this->resolveQualifiedKeyName(), + $entity->{$this->keyName()} + )->firstOrFail(); $entity = $this->cleanupEntity($entity);