Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom too many relations #30

Merged
merged 4 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
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