Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.6] Use value() helper in whenLoaded() #24644

Merged
merged 1 commit into from
Jun 21, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ protected function whenLoaded($relationship, $value = null, $default = null)
}

if (! $this->resource->relationLoaded($relationship)) {
return $default;
return value($default);
}

if (func_num_args() === 1) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Illuminate\Tests\Integration\Http\Fixtures;

class AuthorResourceWithOptionalRelationship extends PostResource
{
public function toArray($request)
{
return [
'name' => $this->name,
'posts_count' => $this->whenLoaded('posts', function () {
return $this->posts->count().' posts';
}, function () {
return 'not loaded';
}),
'latest_post_title' => $this->whenLoaded('posts', function () {
return optional($this->posts->first())->title ?: 'no posts yet';
}, 'not loaded'),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public function toArray($request)
'third' => $this->when(true, function () {
return 'value';
}),
'fourth' => $this->when(false, 'value', 'default'),
'fifth' => $this->when(false, 'value', function () {
return 'default';
}),
];
}
}
26 changes: 26 additions & 0 deletions tests/Integration/Http/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithOptionalData;
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithOptionalMerging;
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithOptionalRelationship;
use Illuminate\Tests\Integration\Http\Fixtures\AuthorResourceWithOptionalRelationship;
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithOptionalPivotRelationship;

/**
Expand Down Expand Up @@ -88,6 +89,8 @@ public function test_resources_may_have_optional_values()
'id' => 5,
'second' => 'value',
'third' => 'value',
'fourth' => 'default',
'fifth' => 'default',
],
]);
}
Expand Down Expand Up @@ -192,6 +195,29 @@ public function test_resources_may_shows_null_for_loaded_relationship_with_value
]);
}

public function test_resources_may_have_optional_relationships_with_default_values()
{
Route::get('/', function () {
return new AuthorResourceWithOptionalRelationship(new Author([
'name' => 'jrrmartin',
]));
});

$response = $this->withoutExceptionHandling()->get(
'/', ['Accept' => 'application/json']
);

$response->assertStatus(200);

$response->assertExactJson([
'data' => [
'name' => 'jrrmartin',
'posts_count' => 'not loaded',
'latest_post_title' => 'not loaded',
],
]);
}

public function test_resources_may_have_optional_pivot_relationships()
{
Route::get('/', function () {
Expand Down