diff --git a/CHANGELOG.md b/CHANGELOG.md index 912c327..8436d5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,14 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/) and [this changelog format](http://keepachangelog.com/). +## Unreleased + +### Added + +- [#30](https://github.com/laravel-json-api/eloquent/pull/30) Allow a wider range of Eloquent relations in + the `QueryToMany` and `QueryToOne` classes. This means packages + like [culturegr/custom-relation](https://github.com/culturegr/custom-relation) will work with this package. + ## [3.0.1] - 2023-04-03 ### Fixed diff --git a/src/QueryToMany.php b/src/QueryToMany.php index 62b680b..f14dd57 100644 --- a/src/QueryToMany.php +++ b/src/QueryToMany.php @@ -20,10 +20,6 @@ namespace LaravelJsonApi\Eloquent; use Illuminate\Database\Eloquent\Model; -use Illuminate\Database\Eloquent\Relations\BelongsToMany as EloquentBelongsToMany; -use Illuminate\Database\Eloquent\Relations\HasMany as EloquentHasMany; -use Illuminate\Database\Eloquent\Relations\HasManyThrough as EloquentHasManyThrough; -use Illuminate\Database\Eloquent\Relations\MorphMany as EloquentMorphMany; use Illuminate\Database\Eloquent\Relations\Relation as EloquentRelation; use Illuminate\Support\LazyCollection; use LaravelJsonApi\Contracts\Pagination\Page; @@ -32,8 +28,6 @@ use LaravelJsonApi\Core\Query\Custom\ExtendedQueryParameters; use LaravelJsonApi\Eloquent\Fields\Relations\ToMany; use LaravelJsonApi\Eloquent\QueryBuilder\JsonApiBuilder; -use LogicException; -use function get_class; use function sprintf; class QueryToMany implements QueryManyBuilder, HasPagination @@ -165,30 +159,22 @@ public function query(): JsonApiBuilder private function getRelation(): EloquentRelation { $name = $this->relation->relationName(); - $relation = $this->model->{$name}(); - if ($relation instanceof EloquentHasMany || - $relation instanceof EloquentBelongsToMany || - $relation instanceof EloquentHasManyThrough || - $relation instanceof EloquentMorphMany - ) { - return $relation; - } + assert(method_exists($this->model, $name), sprintf( + 'Expecting method %s to exist on model %s', + $name, + $this->model::class, + )); - if ($relation instanceof EloquentRelation) { - throw new LogicException(sprintf( - 'Eloquent relation %s on model %s returned a %s relation, which is not a to-many relation.', - $name, - get_class($this->model), - get_class($relation) - )); - } + $relation = $this->model->{$name}(); - throw new LogicException(sprintf( + assert($relation instanceof EloquentRelation, sprintf( 'Expecting method %s on model %s to return an Eloquent relation.', $name, - get_class($this->model) + $this->model::class, )); + + return $relation; } /** diff --git a/src/QueryToOne.php b/src/QueryToOne.php index cd20c11..79d6a72 100644 --- a/src/QueryToOne.php +++ b/src/QueryToOne.php @@ -20,17 +20,12 @@ namespace LaravelJsonApi\Eloquent; use Illuminate\Database\Eloquent\Model; -use Illuminate\Database\Eloquent\Relations\BelongsTo as EloquentBelongsTo; -use Illuminate\Database\Eloquent\Relations\HasOne as EloquentHasOne; -use Illuminate\Database\Eloquent\Relations\HasOneThrough as EloquentHasOneThrough; -use Illuminate\Database\Eloquent\Relations\MorphOne as EloquentMorphOne; use Illuminate\Database\Eloquent\Relations\Relation as EloquentRelation; use LaravelJsonApi\Contracts\Store\QueryOneBuilder; use LaravelJsonApi\Contracts\Store\QueryOneBuilder as QueryOneBuilderContract; use LaravelJsonApi\Core\Query\Custom\ExtendedQueryParameters; use LaravelJsonApi\Eloquent\Fields\Relations\ToOne; use LaravelJsonApi\Eloquent\QueryBuilder\JsonApiBuilder; -use LogicException; use function sprintf; class QueryToOne implements QueryOneBuilder @@ -103,30 +98,22 @@ public function query(): JsonApiBuilder private function getRelation(): EloquentRelation { $name = $this->relation->relationName(); - $relation = $this->model->{$name}(); - if ($relation instanceof EloquentHasOne || - $relation instanceof EloquentBelongsTo || - $relation instanceof EloquentHasOneThrough || - $relation instanceof EloquentMorphOne - ) { - return $relation; - } + assert(method_exists($this->model, $name), sprintf( + 'Expecting method %s to exist on model %s', + $name, + $this->model::class, + )); - if ($relation instanceof EloquentRelation) { - throw new LogicException(sprintf( - 'Eloquent relation %s on model %s returned a %s relation, which is not a to-one relation.', - $name, - get_class($this->model), - get_class($relation) - )); - } + $relation = $this->model->{$name}(); - throw new LogicException(sprintf( + assert($relation instanceof EloquentRelation, sprintf( 'Expecting method %s on model %s to return an Eloquent relation.', $name, - get_class($this->model) + $this->model::class, )); + + return $relation; } /**