Skip to content

Commit 787acb8

Browse files
committed
fix: including nested relations
1 parent 3e6fc42 commit 787acb8

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

src/Drivers/Standard/QueryBuilder.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,16 @@ public function getQualifiedFieldName(string $field): string
340340
*/
341341
public function getRelationModelClass(string $relation): string
342342
{
343-
return get_class((new $this->resourceModelClass)->$relation()->getModel());
343+
$relations = collect(explode('.', $relation));
344+
$relation = $relations[0];
345+
346+
$resourceModel = (new $this->resourceModelClass)->$relation()->getModel();
347+
348+
foreach ($relations->skip(1) as $nestedRelation) {
349+
$resourceModel = $resourceModel->$nestedRelation()->getModel();
350+
}
351+
352+
return get_class($resourceModel);
344353
}
345354

346355
/**

tests/Feature/StandardShowOperationsTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,16 @@ public function getting_a_single_resource_with_included_relation(): void
108108

109109
$this->assertResourceShown($response, $post->fresh('user')->toArray());
110110
}
111+
112+
/** @test */
113+
public function getting_a_single_resource_with_nested_included_relation(): void
114+
{
115+
$post = factory(Post::class)->create(['user_id' => factory(User::class)->create()->id]);
116+
117+
Gate::policy(Post::class, GreenPolicy::class);
118+
119+
$response = $this->get("/api/posts/{$post->id}?include=user.roles");
120+
121+
$this->assertResourceShown($response, $post->fresh('user.roles')->toArray());
122+
}
111123
}

tests/Fixtures/app/Http/Controllers/PostsController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,6 @@ public function exposedScopes(): array
5858
*/
5959
public function includes(): array
6060
{
61-
return ['user'];
61+
return ['user', 'user.roles'];
6262
}
6363
}

tests/Fixtures/app/Models/User.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public function roles()
4242

4343
public function notifications()
4444
{
45-
// $this->roles()->whereDate()
4645
return $this->belongsToMany(Notification::class)->withPivot('meta');
4746
}
4847
}

0 commit comments

Comments
 (0)