Skip to content

Commit ffc31d0

Browse files
themsaidtaylorotwell
authored andcommitted
show null if a relationship is loaded but has value of null (#20969)
1 parent a0e25cd commit ffc31d0

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed

src/Illuminate/Http/Resources/Json/Resource.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ protected function filter($data)
151151
$value->resource instanceof MissingValue)) {
152152
unset($data[$key]);
153153
}
154+
155+
if ($value instanceof self && is_null($value->resource)) {
156+
$data[$key] = null;
157+
}
154158
}
155159

156160
return $data;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Http\Fixtures;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Author extends Model
8+
{
9+
/**
10+
* The attributes that aren't mass assignable.
11+
*
12+
* @var array
13+
*/
14+
protected $guarded = [];
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Http\Fixtures;
4+
5+
use Illuminate\Http\Resources\Json\Resource;
6+
7+
class AuthorResource extends Resource
8+
{
9+
public function toArray($request)
10+
{
11+
return ['name' => $this->name];
12+
}
13+
}

tests/Integration/Http/Fixtures/PostResourceWithOptionalRelationship.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public function toArray($request)
99
return [
1010
'id' => $this->id,
1111
'comments' => new CommentCollection($this->whenLoaded('comments')),
12+
'author' => new AuthorResource($this->whenLoaded('author')),
1213
];
1314
}
1415
}

tests/Integration/Http/ResourceTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminate\Tests\Integration\Http;
44

5+
use Illuminate\Tests\Integration\Http\Fixtures\Author;
56
use Orchestra\Testbench\TestCase;
67
use Illuminate\Support\Facades\Route;
78
use Illuminate\Pagination\LengthAwarePaginator;
@@ -110,6 +111,60 @@ public function test_resources_may_have_optional_relationships()
110111
]);
111112
}
112113

114+
public function test_resources_may_load_optional_relationships()
115+
{
116+
Route::get('/', function () {
117+
$post = new Post([
118+
'id' => 5,
119+
'title' => 'Test Title',
120+
]);
121+
122+
$post->setRelation('author', new Author(['name' => 'jrrmartin']));
123+
124+
return new PostResourceWithOptionalRelationship($post);
125+
});
126+
127+
$response = $this->withoutExceptionHandling()->get(
128+
'/', ['Accept' => 'application/json']
129+
);
130+
131+
$response->assertStatus(200);
132+
133+
$response->assertExactJson([
134+
'data' => [
135+
'id' => 5,
136+
'author' => ['name' => 'jrrmartin']
137+
],
138+
]);
139+
}
140+
141+
public function test_resources_may_shows_null_for_loaded_relationship_with_value_null()
142+
{
143+
Route::get('/', function () {
144+
$post = new Post([
145+
'id' => 5,
146+
'title' => 'Test Title',
147+
]);
148+
149+
$post->setRelation('author', null);
150+
151+
return new PostResourceWithOptionalRelationship($post);
152+
});
153+
154+
$response = $this->withoutExceptionHandling()->get(
155+
'/', ['Accept' => 'application/json']
156+
);
157+
158+
$response->assertStatus(200);
159+
160+
$response->assertExactJson([
161+
'data' => [
162+
'id' => 5,
163+
'author' => null
164+
],
165+
]);
166+
}
167+
113168
public function test_resources_may_have_optional_pivot_relationships()
114169
{
115170
Route::get('/', function () {

0 commit comments

Comments
 (0)