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

[11.x] Support passing default as named parameter in whenLoaded, whenAggregated, whenCounted #51342

Merged
merged 1 commit into from
Aug 1, 2024
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
12 changes: 12 additions & 0 deletions src/Illuminate/Http/Resources/ConditionallyLoadsAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,10 @@ protected function whenLoaded($relationship, $value = null, $default = null)
return;
}

if ($value === null) {
$value = value(...);
}

return value($value, $loadedValue);
}

Expand Down Expand Up @@ -307,6 +311,10 @@ public function whenCounted($relationship, $value = null, $default = null)
return;
}

if ($value === null) {
$value = value(...);
}

return value($value, $this->resource->{$attribute});
}

Expand Down Expand Up @@ -340,6 +348,10 @@ public function whenAggregated($relationship, $column, $aggregate, $value = null
return;
}

if ($value === null) {
$value = value(...);
}

return value($value, $this->resource->{$attribute});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Illuminate\Tests\Integration\Http\Fixtures;

class PostResourceWithOptionalRelationshipUsingNamedParameters extends PostResource
{
public function toArray($request)
{
return [
'id' => $this->id,
'author' => new AuthorResource($this->whenLoaded('author')),
'author_defaulting_to_null' => new AuthorResource($this->whenLoaded('author', default: null)),
'author_name' => $this->whenLoaded('author', fn ($author) => $author->name, 'Anonymous'),
];
}
}
49 changes: 49 additions & 0 deletions tests/Integration/Http/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithOptionalRelationship;
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithOptionalRelationshipAggregates;
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithOptionalRelationshipCounts;
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithOptionalRelationshipUsingNamedParameters;
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithoutWrap;
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithUnlessOptionalData;
use Illuminate\Tests\Integration\Http\Fixtures\ReallyEmptyPostResource;
Expand Down Expand Up @@ -610,6 +611,54 @@ public function testResourceDoesNotThrowErrorWhenUsingEloquentStrictModeAndCheck
]);
}

public function testWhenLoadedUsingNamedDefaultParameterOnMissingRelation()
{
Route::get('/', function () {
$post = new Post(['id' => 1]);

return new PostResourceWithOptionalRelationshipUsingNamedParameters($post);
});

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

$response->assertStatus(200);

$response->assertExactJson([
'data' => [
'id' => 1,
'author_defaulting_to_null' => null,
'author_name' => 'Anonymous',
],
]);
}

public function testWhenLoadedUsingNamedDefaultParameterOnLoadedRelation()
{
Route::get('/', function () {
$post = new Post(['id' => 1]);
$post->setRelation('author', new Author(['name' => 'jrrmartin']));

return new PostResourceWithOptionalRelationshipUsingNamedParameters($post);
});

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

$response->assertStatus(200);

$response->assertExactJson([
'data' => [
'id' => 1,
'author' => ['name' => 'jrrmartin'],
'author_defaulting_to_null' => ['name' => 'jrrmartin'],
'author_name' => 'jrrmartin',
],
]);
}

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