Skip to content

Commit

Permalink
Fixing belongs to nullable. (#327)
Browse files Browse the repository at this point in the history
* Fixing belongs to nullable.

* Apply fixes from StyleCI (#328)
  • Loading branch information
binaryk authored Dec 22, 2020
1 parent d474838 commit dbbeafb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
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

0 comments on commit dbbeafb

Please sign in to comment.