Skip to content

Commit 3daaf05

Browse files
authored
feat: add support for additional Eloquent relations (#30)
This allows packages such as the custom relations one to be used.
1 parent 62e1372 commit 3daaf05

File tree

3 files changed

+28
-47
lines changed

3 files changed

+28
-47
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
All notable changes to this project will be documented in this file. This project adheres to
44
[Semantic Versioning](http://semver.org/) and [this changelog format](http://keepachangelog.com/).
55

6+
## Unreleased
7+
8+
### Added
9+
10+
- [#30](https://github.com/laravel-json-api/eloquent/pull/30) Allow a wider range of Eloquent relations in
11+
the `QueryToMany` and `QueryToOne` classes. This means packages
12+
like [culturegr/custom-relation](https://github.com/culturegr/custom-relation) will work with this package.
13+
614
## [3.0.1] - 2023-04-03
715

816
### Fixed

src/QueryToMany.php

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@
2020
namespace LaravelJsonApi\Eloquent;
2121

2222
use Illuminate\Database\Eloquent\Model;
23-
use Illuminate\Database\Eloquent\Relations\BelongsToMany as EloquentBelongsToMany;
24-
use Illuminate\Database\Eloquent\Relations\HasMany as EloquentHasMany;
25-
use Illuminate\Database\Eloquent\Relations\HasManyThrough as EloquentHasManyThrough;
26-
use Illuminate\Database\Eloquent\Relations\MorphMany as EloquentMorphMany;
2723
use Illuminate\Database\Eloquent\Relations\Relation as EloquentRelation;
2824
use Illuminate\Support\LazyCollection;
2925
use LaravelJsonApi\Contracts\Pagination\Page;
@@ -32,8 +28,6 @@
3228
use LaravelJsonApi\Core\Query\Custom\ExtendedQueryParameters;
3329
use LaravelJsonApi\Eloquent\Fields\Relations\ToMany;
3430
use LaravelJsonApi\Eloquent\QueryBuilder\JsonApiBuilder;
35-
use LogicException;
36-
use function get_class;
3731
use function sprintf;
3832

3933
class QueryToMany implements QueryManyBuilder, HasPagination
@@ -165,30 +159,22 @@ public function query(): JsonApiBuilder
165159
private function getRelation(): EloquentRelation
166160
{
167161
$name = $this->relation->relationName();
168-
$relation = $this->model->{$name}();
169162

170-
if ($relation instanceof EloquentHasMany ||
171-
$relation instanceof EloquentBelongsToMany ||
172-
$relation instanceof EloquentHasManyThrough ||
173-
$relation instanceof EloquentMorphMany
174-
) {
175-
return $relation;
176-
}
163+
assert(method_exists($this->model, $name), sprintf(
164+
'Expecting method %s to exist on model %s',
165+
$name,
166+
$this->model::class,
167+
));
177168

178-
if ($relation instanceof EloquentRelation) {
179-
throw new LogicException(sprintf(
180-
'Eloquent relation %s on model %s returned a %s relation, which is not a to-many relation.',
181-
$name,
182-
get_class($this->model),
183-
get_class($relation)
184-
));
185-
}
169+
$relation = $this->model->{$name}();
186170

187-
throw new LogicException(sprintf(
171+
assert($relation instanceof EloquentRelation, sprintf(
188172
'Expecting method %s on model %s to return an Eloquent relation.',
189173
$name,
190-
get_class($this->model)
174+
$this->model::class,
191175
));
176+
177+
return $relation;
192178
}
193179

194180
/**

src/QueryToOne.php

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,12 @@
2020
namespace LaravelJsonApi\Eloquent;
2121

2222
use Illuminate\Database\Eloquent\Model;
23-
use Illuminate\Database\Eloquent\Relations\BelongsTo as EloquentBelongsTo;
24-
use Illuminate\Database\Eloquent\Relations\HasOne as EloquentHasOne;
25-
use Illuminate\Database\Eloquent\Relations\HasOneThrough as EloquentHasOneThrough;
26-
use Illuminate\Database\Eloquent\Relations\MorphOne as EloquentMorphOne;
2723
use Illuminate\Database\Eloquent\Relations\Relation as EloquentRelation;
2824
use LaravelJsonApi\Contracts\Store\QueryOneBuilder;
2925
use LaravelJsonApi\Contracts\Store\QueryOneBuilder as QueryOneBuilderContract;
3026
use LaravelJsonApi\Core\Query\Custom\ExtendedQueryParameters;
3127
use LaravelJsonApi\Eloquent\Fields\Relations\ToOne;
3228
use LaravelJsonApi\Eloquent\QueryBuilder\JsonApiBuilder;
33-
use LogicException;
3429
use function sprintf;
3530

3631
class QueryToOne implements QueryOneBuilder
@@ -103,30 +98,22 @@ public function query(): JsonApiBuilder
10398
private function getRelation(): EloquentRelation
10499
{
105100
$name = $this->relation->relationName();
106-
$relation = $this->model->{$name}();
107101

108-
if ($relation instanceof EloquentHasOne ||
109-
$relation instanceof EloquentBelongsTo ||
110-
$relation instanceof EloquentHasOneThrough ||
111-
$relation instanceof EloquentMorphOne
112-
) {
113-
return $relation;
114-
}
102+
assert(method_exists($this->model, $name), sprintf(
103+
'Expecting method %s to exist on model %s',
104+
$name,
105+
$this->model::class,
106+
));
115107

116-
if ($relation instanceof EloquentRelation) {
117-
throw new LogicException(sprintf(
118-
'Eloquent relation %s on model %s returned a %s relation, which is not a to-one relation.',
119-
$name,
120-
get_class($this->model),
121-
get_class($relation)
122-
));
123-
}
108+
$relation = $this->model->{$name}();
124109

125-
throw new LogicException(sprintf(
110+
assert($relation instanceof EloquentRelation, sprintf(
126111
'Expecting method %s on model %s to return an Eloquent relation.',
127112
$name,
128-
get_class($this->model)
113+
$this->model::class,
129114
));
115+
116+
return $relation;
130117
}
131118

132119
/**

0 commit comments

Comments
 (0)