Skip to content
This repository was archived by the owner on Feb 14, 2023. It is now read-only.

Commit 458b347

Browse files
committed
Fix included items being repeated on JsonApiResource
1 parent 449f9cc commit 458b347

File tree

2 files changed

+23
-37
lines changed

2 files changed

+23
-37
lines changed

src/Http/Resources/CollectsWithIncludes.php

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@
22

33
namespace SkoreLabs\JsonApi\Http\Resources;
44

5-
use Illuminate\Support\Arr;
6-
use Illuminate\Support\Collection;
7-
85
/**
96
* @property \Illuminate\Support\Collection $collection
10-
* @property array $with
117
*/
128
trait CollectsWithIncludes
139
{
@@ -20,31 +16,7 @@ protected function withIncludes()
2016
{
2117
/** @var \SkoreLabs\JsonApi\Http\Resources\JsonApiResource $jsonResource */
2218
foreach ($this->collection->toArray() as $jsonResource) {
23-
if ($jsonResource->with) {
24-
$this->addIncluded(Arr::get($jsonResource->with, 'included'));
25-
}
26-
}
27-
28-
$included = $this->uniqueIncludes();
29-
30-
if ($included) {
31-
$this->with['included'] = $included;
19+
$this->addIncluded($jsonResource);
3220
}
3321
}
34-
35-
/**
36-
* Post-process for unique relationship includes.
37-
*
38-
* @return array
39-
*/
40-
protected function uniqueIncludes()
41-
{
42-
$includedCollection = Collection::make(
43-
Arr::get($this->with, 'included', [])
44-
)->flatten();
45-
46-
return $includedCollection->unique(static function (JsonApiResource $resource) {
47-
return implode('', $resource->getResourceIdentifier());
48-
})->values()->all();
49-
}
5022
}

src/Http/Resources/RelationshipsWithIncludes.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,24 +73,38 @@ protected function processModelRelation(Model $model)
7373
{
7474
$modelResource = new JsonApiResource($model, $this->authorize);
7575

76-
$this->addIncluded([$modelResource],
77-
Arr::get($modelResource->with, 'included', [])
78-
);
76+
$this->addIncluded($modelResource);
7977

8078
return $modelResource->getResourceIdentifier();
8179
}
8280

8381
/**
8482
* Set included data to resource's with.
8583
*
86-
* @param array $arrays,...
84+
* @param $resource
8785
*
8886
* @return void
8987
*/
90-
protected function addIncluded(...$arrays)
88+
protected function addIncluded(JsonApiResource $resource)
9189
{
92-
Arr::set($this->with, 'included',
93-
array_merge(Arr::get($this->with, 'included', []), ...$arrays)
94-
);
90+
$itemsCol = Collection::make([
91+
$resource,
92+
array_values($this->getIncluded()),
93+
array_values($resource->getIncluded())
94+
])->flatten();
95+
96+
Arr::set($this->with, 'included', $itemsCol->unique(static function ($resource) {
97+
return implode('', $resource->getResourceIdentifier());
98+
})->values()->all());
99+
}
100+
101+
/**
102+
* Get resource included relationships.
103+
*
104+
* @return array
105+
*/
106+
public function getIncluded()
107+
{
108+
return Arr::get($this->with, 'included', []);
95109
}
96110
}

0 commit comments

Comments
 (0)