Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[12.x] JSON:API Resource#57571

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
crynobone wants to merge79 commits into12.x
base:12.x
Choose a base branch
Loading
fromjson-api-resource
Draft
Show file tree
Hide file tree
Changes from1 commit
Commits
Show all changes
79 commits
Select commitHold shift + click to select a range
fce2a2e
wip
crynoboneOct 23, 2025
a00cd30
wip
crynoboneOct 28, 2025
6457e72
Apply fixes from StyleCI
StyleCIBotOct 28, 2025
5d15ab7
wip
crynoboneOct 28, 2025
2214970
Merge remote-tracking branch 'origin/json-api-resource' into json-api…
crynoboneOct 28, 2025
6116f06
Apply fixes from StyleCI
StyleCIBotOct 28, 2025
c703cf8
wip
crynoboneOct 28, 2025
b219e6d
wip
crynoboneOct 29, 2025
8d2da32
Apply fixes from StyleCI
StyleCIBotOct 29, 2025
e7de423
wip
crynoboneOct 29, 2025
882ed31
Apply fixes from StyleCI
StyleCIBotOct 29, 2025
3d75b4c
wip
crynoboneOct 29, 2025
4aa295d
wip
crynoboneOct 29, 2025
d945d9f
Apply fixes from StyleCI
StyleCIBotOct 29, 2025
c2d7dfc
wip
crynoboneOct 30, 2025
50c0193
Merge remote-tracking branch 'origin/json-api-resource' into json-api…
crynoboneOct 30, 2025
2b69984
wip
crynoboneOct 30, 2025
b006401
Apply fixes from StyleCI
StyleCIBotOct 30, 2025
174b4a8
wip
crynoboneOct 30, 2025
13fedeb
Apply fixes from StyleCI
StyleCIBotOct 30, 2025
5c15920
wip
crynoboneOct 30, 2025
0319cbe
wip
crynoboneOct 30, 2025
a3ee8f7
wip
crynoboneOct 30, 2025
b610d9c
wip
crynoboneOct 30, 2025
b1c0b2a
Apply fixes from StyleCI
StyleCIBotOct 30, 2025
e7a8d6e
wip
crynoboneOct 30, 2025
f153b5e
Apply fixes from StyleCI
StyleCIBotOct 30, 2025
dab4d40
wip
crynoboneOct 30, 2025
c742be0
wip
crynoboneOct 30, 2025
1fc4aaa
Merge remote-tracking branch 'origin/json-api-resource' into json-api…
crynoboneOct 30, 2025
bc8e87a
wip
crynoboneOct 30, 2025
99bf9a6
wip
crynoboneOct 30, 2025
f9bfd62
wip
crynoboneOct 30, 2025
62c56c0
Apply fixes from StyleCI
StyleCIBotOct 30, 2025
e9c68b0
wip
crynoboneOct 30, 2025
f0f58d9
wip
crynoboneOct 30, 2025
e1212fb
Apply fixes from StyleCI
StyleCIBotOct 30, 2025
1928b74
wip
crynoboneOct 30, 2025
4e27c4b
wip
crynoboneOct 30, 2025
387e2a4
wip
crynoboneOct 30, 2025
73c243a
wip
crynoboneOct 30, 2025
ab05c9f
wip
crynoboneOct 30, 2025
d8ecdbc
wip
crynoboneOct 30, 2025
9ce5b07
Apply fixes from StyleCI
StyleCIBotOct 30, 2025
e1f0bbd
wip
crynoboneOct 30, 2025
5b3f81c
wip
crynoboneOct 30, 2025
eda7c76
wip
crynoboneOct 30, 2025
9b279bb
Apply suggestions from code review
crynoboneOct 30, 2025
6275377
wip
crynoboneOct 30, 2025
280c18c
wip
crynoboneOct 30, 2025
ac0ec25
wip
crynoboneOct 30, 2025
f466566
Apply fixes from StyleCI
StyleCIBotOct 30, 2025
d45e57b
wip
crynoboneOct 30, 2025
dc0ec65
wip
crynoboneOct 30, 2025
f4bf030
wip
crynoboneOct 30, 2025
e5c2f88
Apply fixes from StyleCI
StyleCIBotOct 30, 2025
4a8cfc2
wip
crynoboneOct 30, 2025
8f9d151
Apply fixes from StyleCI
StyleCIBotOct 30, 2025
76cc84e
wip
crynoboneOct 30, 2025
86884a9
Apply fixes from StyleCI
StyleCIBotOct 30, 2025
afb51d1
wip
crynoboneOct 30, 2025
5f52c26
Apply fixes from StyleCI
StyleCIBotOct 30, 2025
4284619
wip
crynoboneOct 30, 2025
504193d
Apply fixes from StyleCI
StyleCIBotOct 30, 2025
1a51b54
wip
crynoboneOct 30, 2025
0ed469a
wip
crynoboneOct 30, 2025
a17ac8d
remove asJsonApi from base resource
taylorotwellOct 30, 2025
6836018
update exception
taylorotwellOct 30, 2025
5459428
formatting
taylorotwellOct 30, 2025
4a5e360
fix test
taylorotwellOct 30, 2025
215b9ba
formatting
taylorotwellOct 30, 2025
573a57a
formatting
taylorotwellOct 30, 2025
115ed94
formatting
taylorotwellOct 30, 2025
1da745b
rename trait
taylorotwellOct 30, 2025
8e20f20
Merge remote-tracking branch 'origin/12.x' into json-api-resource
crynoboneOct 31, 2025
4abf43a
[JSON:API] Add `toAttributes()` method (#57603)
crynoboneOct 31, 2025
4451594
[JSON:API] Allows `JsonApiResource::$attributes` (#57638)
crynoboneNov 3, 2025
be536df
[JSON:API] Supports `JsonApiResource::toRelationships()` (#57646)
crynoboneNov 7, 2025
ea1ea07
formatting
taylorotwellNov 7, 2025
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
PrevPrevious commit
NextNext commit
wip
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
@crynobone
crynobone committedOct 30, 2025
commit2b6998478ba2d8786450d840e096df09608869a2
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonApiResource;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use JsonSerializable;
Expand All@@ -17,7 +18,12 @@ trait ResolvesJsonApiSpecifications
/**
* @var \WeakMap|null
*/
protected $cachedLoadedRelationships;
protected $cachedLoadedRelationshipsMap;

/**
* @var array
*/
protected array $cachedLoadedRelationshipsIdentifier = [];

/**
* Resolves `attributes` for the resource.
Expand DownExpand Up@@ -51,67 +57,77 @@ public function resolveResourceData(Request $request): array
'type' => $this->resolveResourceType($request),
...(new Collection([
'attributes' => $this->resolveResourceAttributes($request),
'relationships' => $this->resolveResourceRelationships($request),
'relationships' => $this->resolveResourceRelationshipsIdentifiers($request),
'links' => $this->resolveResourceLinks($request),
'meta' => $this->resolveMetaInformations($request),
]))->filter()->map(fn ($value) => (object) $value),
];
}

protected function resolveResourceRelationships(Request $request):array
protected function resolveResourceRelationships(Request $request):void
{
if (!$this->resource instanceofModel) {
return [];
if ($this->cachedLoadedRelationships instanceofWeakMap) {
return;
}

if (is_null($this->cachedLoadedRelationships)) {
$this->cachedLoadedRelationships = new WeakMap;
}
$this->cachedLoadedRelationshipsMap = new WeakMap;

return [
'data' => (new Collection($this->resource->getRelations()))
->mapWithKeys(function ($relations, $key) {
if ($relations instanceof Collection) {
if ($relations->isEmpty()) {
return [$key => $relations];
}
$this->cachedLoadedRelationshipsIdentifier = (new Collection($this->resource->getRelations()))
->mapWithKeys(function ($relations, $key) {
if ($relations instanceof Collection) {
if ($relations->isEmpty()) {
return [$key => $relations];
}

$key = static::getResourceTypeFromEloquent($relations->first());

$key = static::getResourceTypeFromEloquent($relations->first());
return [$key => $relations->map(function ($relation) use ($key) {
return transform([$key, static::getResourceIdFromEloquent($relation)], function ($uniqueKey) use ($relation) {
$this->cachedLoadedRelationshipsMap[$relation] = $uniqueKey;

$relations->each(function ($relation) use ($key) {
$this->cachedLoadedRelationships[$relation] = [$key, $relation->getKey()];
return ['id' => $uniqueKey[1], 'type' => $uniqueKey[0]];
});
})];
}

return [$key => $relations->map(function ($relation) use ($key) {
return tap([$key, static::getResourceIdFromEloquent($relation)], function ($uniqueKey) use ($relation) {
$this->cachedLoadedRelationships[$relation] = $uniqueKey;
});
})];
return transform(
[static::getResourceTypeFromEloquent($relation), static::getResourceIdFromEloquent($relation)],
function ($uniqueKey) {
$this->cachedLoadedRelationshipsMap[$relation] = $uniqueKey;

return ['id' => $uniqueKey[1], 'type' => $uniqueKey[0]];
}
);
})->all();
}

return tap(
[static::getResourceTypeFromEloquent($relation), static::getResourceIdFromEloquent($relation)],
function ($uniqueKey) use ($relations) {
$this->cachedLoadedRelationships[$relations] = $uniqueKey;
}
);
}),
protected function resolveResourceRelationshipsIdentifiers(Request $request): array
{
if (! $this->resource instanceof Model) {
return [];
}

$this->resolveResourceRelationships($request);

return [
'data' => $this->cachedLoadedRelationshipsIdentifier,
];
}

protected function resolveResourceIncluded(Request $request): array
public function resolveResourceIncluded(Request $request): array
{
$relations =[];
$relations =new Collection();

foreach ($this->cachedLoadedRelationships as $relation => $uniqueKey) {
$relations[] = [
foreach ($this->cachedLoadedRelationshipsMap as $relation => $uniqueKey) {
$resource = rescue(fn () => $relation->toResource(), new JsonApiResource($relation), false);
$relations->push([
'id' => $uniqueKey[1],
'type' => $uniqueKey[0],
'attributes' =>rescue(fn () => $relation->toResource()->toArray($request), $relation->toArray(), false),
];
'attributes' =>$resource->toArray($request),
]);
}

return $relations;
return $relations->uniqueStrict(fn ($relation): array => [$relation['id'], $relation['type']])->all();
}

/**
Expand Down
2 changes: 1 addition & 1 deletionsrc/Illuminate/Http/Resources/Json/JsonApiResource.php
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -81,7 +81,7 @@ public static function configure(?string $version = null, array $ext = [], array
public function resolve($request = null)
{
return [
'data' => $this->resolveResourceToJsonApiSchema($request),
'data' => $this->resolveResourceData($request),
];
}

Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,6 +7,25 @@

class JsonApiResourceCollection extends AnonymousResourceCollection
{

/**
* @param Request $request
* @return array{included?: array<int, JsonApiResource>, jsonapi: ServerImplementation}
*/
public function with($request)
{
return array_filter([
'included' => $this->collection
->map(fn ($resource) => $resource->resolveResourceIncluded($request))
->flatten(depth: 1)
->uniqueStrict(fn ($relation): array => [$relation['id'], $relation['type']])
->all(),
...($implementation = JsonApiResource::$jsonApiInformation)
? ['jsonapi' => $implementation]
: [],
]);
}

/**
* Transform the resource into a JSON array.
*
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp