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

Commit cd7ea69

Browse files
committed
Fix & change includes (adding pivot models)
1 parent ddbdfde commit cd7ea69

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

src/Http/Resources/CollectsWithIncludes.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace SkoreLabs\JsonApi\Http\Resources;
44

5+
use Illuminate\Support\Arr;
6+
use Illuminate\Support\Collection;
7+
58
/**
69
* @property \Illuminate\Support\Collection $collection
710
*/
@@ -14,9 +17,20 @@ trait CollectsWithIncludes
1417
*/
1518
protected function withIncludes()
1619
{
20+
$collectionIncludes = Collection::make(
21+
Arr::get($this->with, 'includes', [])
22+
);
23+
1724
/** @var \SkoreLabs\JsonApi\Http\Resources\JsonApiResource $jsonResource */
1825
foreach ($this->collection->toArray() as $jsonResource) {
19-
$this->addIncluded($jsonResource);
26+
/** @var \SkoreLabs\JsonApi\Http\Resources\JsonApiResource $resource */
27+
foreach ($jsonResource->getIncluded() as $resource) {
28+
$collectionIncludes->push($resource);
29+
}
2030
}
31+
32+
Arr::set($this->with, 'includes', $this->checkUniqueness(
33+
$collectionIncludes
34+
)->values()->all());
2135
}
2236
}

src/Http/Resources/RelationshipsWithIncludes.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace SkoreLabs\JsonApi\Http\Resources;
44

5-
use Illuminate\Database\Eloquent\Collection;
5+
use Illuminate\Database\Eloquent\Collection as DatabaseCollection;
66
use Illuminate\Database\Eloquent\Model;
7-
use Illuminate\Database\Eloquent\Relations\Pivot;
87
use Illuminate\Support\Arr;
8+
use Illuminate\Support\Collection;
99

1010
/**
1111
* @property mixed $resource
@@ -45,7 +45,7 @@ protected function attachRelations(Model $model)
4545
$relations = array_filter($model->getRelations());
4646

4747
foreach ($relations as $relation => $relationObj) {
48-
if ($relationObj instanceof Collection) {
48+
if ($relationObj instanceof DatabaseCollection) {
4949
/** @var \Illuminate\Database\Eloquent\Model $relationModel */
5050
foreach ($relationObj->all() as $relationModel) {
5151
$this->relationships[$relation]['data'][] = $this->processModelRelation(
@@ -54,7 +54,7 @@ protected function attachRelations(Model $model)
5454
}
5555
}
5656

57-
if ($relationObj instanceof Model && !$relationObj instanceof Pivot) {
57+
if ($relationObj instanceof Model) {
5858
$this->relationships[$relation]['data'] = $this->processModelRelation(
5959
$relationObj
6060
);
@@ -72,10 +72,14 @@ protected function attachRelations(Model $model)
7272
protected function processModelRelation(Model $model)
7373
{
7474
$modelResource = new JsonApiResource($model, $this->authorize);
75+
$modelIdentifier = $modelResource->getResourceIdentifier();
7576

76-
$this->addIncluded($modelResource);
77+
if (!empty(Arr::get($modelIdentifier, $model->getKeyName(), null))) {
78+
$this->addIncluded($modelResource);
79+
return $modelIdentifier;
80+
}
7781

78-
return $modelResource->getResourceIdentifier();
82+
return [];
7983
}
8084

8185
/**
@@ -93,9 +97,9 @@ protected function addIncluded(JsonApiResource $resource)
9397
array_values($resource->getIncluded()),
9498
])->flatten();
9599

96-
Arr::set($this->with, 'included', $itemsCol->unique(static function ($resource) {
97-
return implode('', $resource->getResourceIdentifier());
98-
})->values()->all());
100+
Arr::set($this->with, 'included', $this->checkUniqueness(
101+
$itemsCol
102+
)->values()->all());
99103
}
100104

101105
/**
@@ -107,4 +111,18 @@ public function getIncluded()
107111
{
108112
return Arr::get($this->with, 'included', []);
109113
}
114+
115+
/**
116+
* Check and return unique resources on a collection.
117+
*
118+
* @param \Illuminate\Support\Collection
119+
*
120+
* @return \Illuminate\Support\Collection
121+
*/
122+
protected function checkUniqueness(Collection $collection)
123+
{
124+
return $collection->unique(static function ($resource) {
125+
return implode('', $resource->getResourceIdentifier());
126+
});
127+
}
110128
}

0 commit comments

Comments
 (0)