Skip to content

Commit

Permalink
fix: including nested relations
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzarbn committed Oct 26, 2022
1 parent 3e6fc42 commit 787acb8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/Drivers/Standard/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,16 @@ public function getQualifiedFieldName(string $field): string
*/
public function getRelationModelClass(string $relation): string
{
return get_class((new $this->resourceModelClass)->$relation()->getModel());
$relations = collect(explode('.', $relation));
$relation = $relations[0];

$resourceModel = (new $this->resourceModelClass)->$relation()->getModel();

foreach ($relations->skip(1) as $nestedRelation) {
$resourceModel = $resourceModel->$nestedRelation()->getModel();
}

return get_class($resourceModel);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/Feature/StandardShowOperationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,16 @@ public function getting_a_single_resource_with_included_relation(): void

$this->assertResourceShown($response, $post->fresh('user')->toArray());
}

/** @test */
public function getting_a_single_resource_with_nested_included_relation(): void
{
$post = factory(Post::class)->create(['user_id' => factory(User::class)->create()->id]);

Gate::policy(Post::class, GreenPolicy::class);

$response = $this->get("/api/posts/{$post->id}?include=user.roles");

$this->assertResourceShown($response, $post->fresh('user.roles')->toArray());
}
}
2 changes: 1 addition & 1 deletion tests/Fixtures/app/Http/Controllers/PostsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ public function exposedScopes(): array
*/
public function includes(): array
{
return ['user'];
return ['user', 'user.roles'];
}
}
1 change: 0 additions & 1 deletion tests/Fixtures/app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public function roles()

public function notifications()
{
// $this->roles()->whereDate()
return $this->belongsToMany(Notification::class)->withPivot('meta');
}
}

0 comments on commit 787acb8

Please sign in to comment.