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

Commit 33903ec

Browse files
committed
Relations with custom API resource classes
1 parent b366312 commit 33903ec

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [2.0.2] - 2020-11-11
11+
12+
### Added
13+
14+
- Relations with custom API resource classes
15+
1016
## [2.0.1] - 2020-07-02
1117

1218
### Added

src/Http/Resources/RelationshipsWithIncludes.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ protected function attachRelations(Model $model)
7878
*/
7979
protected function processModelRelation(Model $model)
8080
{
81-
$modelResource = new JsonApiResource($model, $this->authorise);
81+
$modelResourceClass = $this->getModelResource($model);
82+
/** @var \SkoreLabs\JsonApi\Http\Resources\JsonApiResource $modelResource */
83+
$modelResource = new $modelResourceClass($model, $this->authorise);
8284
$modelIdentifier = $modelResource->getResourceIdentifier();
8385

8486
if (!empty(Arr::get($modelIdentifier, $model->getKeyName(), null))) {
@@ -137,4 +139,17 @@ protected function checkUniqueness(Collection $collection)
137139
return implode('', $resource->getResourceIdentifier());
138140
});
139141
}
142+
143+
/**
144+
* Get API resource from model.
145+
*
146+
* @param \Illuminate\Database\Eloquent\Model $model
147+
* @return string
148+
*/
149+
protected function getModelResource(Model $model)
150+
{
151+
return defined(get_class($model) . '::JSON_SERIALIZER')
152+
? $model::JSON_SERIALIZER
153+
: JsonApiResource::class;
154+
}
140155
}

0 commit comments

Comments
 (0)