Skip to content

Commit c417c6a

Browse files
authored
Support passing default as named parameter in whenLoaded, whenAggregated, whenCounted (#51342)
1 parent efe4fde commit c417c6a

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

src/Illuminate/Http/Resources/ConditionallyLoadsAttributes.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,10 @@ protected function whenLoaded($relationship, $value = null, $default = null)
276276
return;
277277
}
278278

279+
if ($value === null) {
280+
$value = value(...);
281+
}
282+
279283
return value($value, $loadedValue);
280284
}
281285

@@ -307,6 +311,10 @@ public function whenCounted($relationship, $value = null, $default = null)
307311
return;
308312
}
309313

314+
if ($value === null) {
315+
$value = value(...);
316+
}
317+
310318
return value($value, $this->resource->{$attribute});
311319
}
312320

@@ -340,6 +348,10 @@ public function whenAggregated($relationship, $column, $aggregate, $value = null
340348
return;
341349
}
342350

351+
if ($value === null) {
352+
$value = value(...);
353+
}
354+
343355
return value($value, $this->resource->{$attribute});
344356
}
345357

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Http\Fixtures;
4+
5+
class PostResourceWithOptionalRelationshipUsingNamedParameters extends PostResource
6+
{
7+
public function toArray($request)
8+
{
9+
return [
10+
'id' => $this->id,
11+
'author' => new AuthorResource($this->whenLoaded('author')),
12+
'author_defaulting_to_null' => new AuthorResource($this->whenLoaded('author', default: null)),
13+
'author_name' => $this->whenLoaded('author', fn ($author) => $author->name, 'Anonymous'),
14+
];
15+
}
16+
}

tests/Integration/Http/ResourceTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithOptionalRelationship;
3838
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithOptionalRelationshipAggregates;
3939
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithOptionalRelationshipCounts;
40+
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithOptionalRelationshipUsingNamedParameters;
4041
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithoutWrap;
4142
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithUnlessOptionalData;
4243
use Illuminate\Tests\Integration\Http\Fixtures\ReallyEmptyPostResource;
@@ -610,6 +611,54 @@ public function testResourceDoesNotThrowErrorWhenUsingEloquentStrictModeAndCheck
610611
]);
611612
}
612613

614+
public function testWhenLoadedUsingNamedDefaultParameterOnMissingRelation()
615+
{
616+
Route::get('/', function () {
617+
$post = new Post(['id' => 1]);
618+
619+
return new PostResourceWithOptionalRelationshipUsingNamedParameters($post);
620+
});
621+
622+
$response = $this->withoutExceptionHandling()->get(
623+
'/', ['Accept' => 'application/json']
624+
);
625+
626+
$response->assertStatus(200);
627+
628+
$response->assertExactJson([
629+
'data' => [
630+
'id' => 1,
631+
'author_defaulting_to_null' => null,
632+
'author_name' => 'Anonymous',
633+
],
634+
]);
635+
}
636+
637+
public function testWhenLoadedUsingNamedDefaultParameterOnLoadedRelation()
638+
{
639+
Route::get('/', function () {
640+
$post = new Post(['id' => 1]);
641+
$post->setRelation('author', new Author(['name' => 'jrrmartin']));
642+
643+
return new PostResourceWithOptionalRelationshipUsingNamedParameters($post);
644+
});
645+
646+
$response = $this->withoutExceptionHandling()->get(
647+
'/', ['Accept' => 'application/json']
648+
);
649+
650+
$response->assertStatus(200);
651+
652+
$response->assertExactJson([
653+
'data' => [
654+
'id' => 1,
655+
'author' => ['name' => 'jrrmartin'],
656+
'author_defaulting_to_null' => ['name' => 'jrrmartin'],
657+
'author_name' => 'jrrmartin',
658+
],
659+
]);
660+
}
661+
613662
public function testResourcesMayHaveOptionalPivotRelationshipsWithCustomAccessor()
614663
{
615664
Route::get('/', function () {

0 commit comments

Comments
 (0)