Skip to content

Commit

Permalink
Merge branch 'release/3.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
lindyhopchris committed Oct 29, 2023
2 parents 3a69d13 + 06741bf commit 5398cf6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 47 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
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

## [3.1.0] - 2023-10-29

### 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
Expand Down
34 changes: 10 additions & 24 deletions src/QueryToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
}

/**
Expand Down
33 changes: 10 additions & 23 deletions src/QueryToOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 5398cf6

Please sign in to comment.