Skip to content

Commit 924e347

Browse files
committed
Add default via relationship for related
1 parent 6b2f460 commit 924e347

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

src/Http/Requests/InteractWithRepositories.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,6 @@ public function findRelatedQuery($relatedRepository = null, $relatedRepositoryId
177177
->whereKey($relatedRepositoryId ?? request('relatedRepositoryId'));
178178
}
179179

180-
protected function findPivot(RestifyRequest $request, $model)
181-
{
182-
$pivot = $model->{$request->relatedRepository}()->getPivotAccessor();
183-
184-
return $model->{$request->viaRelationship}()
185-
->withoutGlobalScopes()
186-
->lockForUpdate()
187-
->findOrFail($request->relatedRepositoryId)->{$pivot};
188-
}
189-
190180
public function viaParentModel()
191181
{
192182
$parent = $this->repository($this->viaRepository);
@@ -196,7 +186,7 @@ public function viaParentModel()
196186

197187
public function viaQuery()
198188
{
199-
return $this->viaParentModel()->{$this->viaRelationship}();
189+
return $this->viaParentModel()->{$this->viaRelationship ?? request('repository')}();
200190
}
201191

202192
/**

src/Http/Requests/RestifyRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@ public function isStoreRequest()
6161

6262
public function isViaRepository()
6363
{
64-
return $this->viaRepository && $this->viaRepositoryId && $this->viaRelationship;
64+
return $this->viaRepository && $this->viaRepositoryId;
6565
}
6666
}

tests/Controllers/RelatedIndexControllerTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ public function test_can_list_posts_belongs_to_a_user()
2525
$this->assertCount(10, $response->json('data'));
2626
}
2727

28+
public function test_can_list_posts_belongs_to_a_user_without_via_relationship_because_get_default_main_repository()
29+
{
30+
$this->mockUsers();
31+
$this->mockPosts(1, 10);
32+
33+
$this->mockPosts(
34+
factory(User::class)->create()->id
35+
);
36+
37+
$response = $this->getJson('restify-api/posts?viaRepository=users&viaRepositoryId=1')
38+
->assertStatus(200);
39+
40+
$this->assertCount(10, $response->json('data'));
41+
}
42+
2843
public function test_can_show_post_belongs_to_a_user()
2944
{
3045
factory(User::class)->create();

0 commit comments

Comments
 (0)