Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Fields/EagerField.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ public function resolve($repository, $attribute = null)

$relatedModel = $model->{$this->relation}()->first();

if (is_null($relatedModel)) {
$this->value = null;

return $this;
}

try {
$this->value = $this->repositoryClass::resolveWith($relatedModel)
->allowToShow(app(Request::class))
Expand Down
17 changes: 17 additions & 0 deletions tests/Fields/BelongsToFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,23 @@ public function test_unauthorized_see_relationship()
});
}

public function test_dont_show_key_when_nullable_related()
{
$_SERVER['restify.users.show'] = true;

Gate::policy(User::class, UserPolicy::class);

tap(factory(Post::class)->create([
'user_id' => null,
]), function ($post) {
$this->get(PostWithUserRepository::uriKey()."/{$post->id}?related=user")
->assertJsonFragment([
'user' => null,
])
->assertOk();
});
}

public function test_field_used_when_storing()
{
tap(factory(User::class)->create(), function ($user) {
Expand Down