Skip to content
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
3 changes: 2 additions & 1 deletion src/Resources/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ public function toArray(mixed $request, bool $included = true): array
{
$value = $this->whenIncluded && !$included
? new MissingValue
: value($this->value);
: value($this->value)
?? new MissingValue;

if ($this->asCollection && !is_subclass_of($this->resource, ResourceCollection::class)) {
$resource = $this->resource::collection($value);
Expand Down
1 change: 1 addition & 0 deletions src/Support/Values.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static function mergeValues(iterable $data): array
$index++;

if (is_iterable($value)) {
/** @var array<array-key, mixed> $value */
$data[$key] = self::mergeValues($value);

continue;
Expand Down
2 changes: 2 additions & 0 deletions tests/Feature/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ public function testSchema()
$post = new Skeleton(PostResource::class, 'post', ['state', 'title', 'content']);
$comment = new Skeleton(CommentResource::class, 'comment', ['content']);

$user->relationships['main-post'] = $post;
$user->relationships['posts'] = $post;
$user->relationships['comments'] = $comment;
$user->loads['main-post'] = 'post';
$user->loads['posts'] = 'posts';
$user->loads['comments'] = [
'comments' => fn(Builder $q) => $q->where('content', 'like', '%e%')
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/User/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ private function getJsonResult(User $user, ?array $attributes = null, ?array $re
'email' => $user->email,
], array_fill_keys($attributes ?? ['name', 'email'], true))),
'relationships' => [
'main-post' => [],
'posts' => array_filter([
'data' => $withIncluded ? $user->posts->map(fn(Post $post) => ['type' => 'post', 'id' => $post->id])->all() : null,
'links' => [
Expand Down
33 changes: 33 additions & 0 deletions tests/Unit/Resources/RelationshipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,23 @@ public function testToArrayMissingValue()
], $relation->toArray(new Request(), false));
}

public function testToArrayNullValue()
{
$resource = $this->getJsonResourceNullValue();
$relation = (new Relationship($resource::class, fn() => $resource->resource))->withLinks(fn() => [
'self' => 'link'
])->withMeta(fn() => [
'hash' => 'azerty'
]);

$this->assertEquals([
'data' => [
'links' => ['self' => 'link'],
'meta' => ['hash' => 'azerty'],
],
], $relation->toArray(new Request(), false));
}

public function testCustomValue()
{
$relation = (new Relationship(Collection::class, fn() => ['foo' => 'bar']))
Expand Down Expand Up @@ -217,6 +234,22 @@ public function toArray($request, bool $minimal = false): array
};
}

private function getJsonResourceNullValue()
{
return new class(null) extends JsonResource implements Resourceable {
public function toArray($request, bool $minimal = false): array
{
return [
'type' => 'my-model',
'id' => $this->id,
'attributes' => [
'foo' => 'bar'
]
];
}
};
}

private function getResourceCollection()
{
return new class(collect()) extends ResourceCollection implements Resourceable {
Expand Down
2 changes: 2 additions & 0 deletions tests/app/Http/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ protected function toResourceMeta(Request $request): ?iterable
protected function toRelationships(Request $request): iterable
{
return [
'main-post' => $this->one(PostResource::class, fn() => $this->resource->post)
->withLoad('post'),
'posts' => PostResource::relationship(fn() => $this->resource->posts, fn() => [
'self' => "https://api.example.com/user/{$this->resource->id}/relationships/posts",
'related' => "https://api.example.com/user/{$this->resource->id}/posts",
Expand Down
7 changes: 7 additions & 0 deletions tests/app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Test\app\Factories\UserFactory;

Expand All @@ -14,6 +15,7 @@
* @property \DateTimeInterface $created_at
* @property \DateTimeInterface $updated_at
*
* @property-read Post|null $post
* @property-read Post[]|\Illuminate\Support\Collection<Post> $posts
* @property-read Comment[]|\Illuminate\Support\Collection<Comment> $comments
*/
Expand All @@ -26,6 +28,11 @@ protected static function newFactory(): UserFactory
return new UserFactory();
}

public function post(): BelongsTo
{
return $this->belongsTo(Post::class);
}

public function posts(): HasMany
{
return $this->hasMany(Post::class);
Expand Down
15 changes: 15 additions & 0 deletions tests/app/migrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,26 @@ public function tables()
];
}

public function changes()
{
return [
'users' => function (Blueprint $table) {
$table->foreignId('post_id')
->nullable()
->after('password')
->constrained('posts');
},
];
}

public function up()
{
foreach ($this->tables() as $table => $blueprint) {
Schema::create($table, $blueprint);
}
foreach ($this->changes() as $table => $blueprint) {
Schema::table($table, $blueprint);
}
}

public function down()
Expand Down