Skip to content

Commit

Permalink
Use value() helper in whenLoaded() (#24644)
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigopedra authored and taylorotwell committed Jun 21, 2018
1 parent c87a944 commit 2c02bf7
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
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

0 comments on commit 2c02bf7

Please sign in to comment.