Skip to content

Commit d9b631d

Browse files
committed
[graphql] ModelHelper::getRelation() will return Relation without constraints for Builder (this is required for sorting, overwise it will create query with foreign_key IS NULL AND foreign_key IS NOT NULL).
1 parent 9d378ca commit d9b631d

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/Helpers/ModelHelper.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
use function sprintf;
1515

1616
class ModelHelper {
17+
protected bool $builder;
1718
protected Model $model;
1819

1920
public function __construct(Builder|Model $model) {
20-
$this->model = $model instanceof Builder
21+
$this->builder = $model instanceof Builder;
22+
$this->model = $model instanceof Builder
2123
? $model->getModel()
2224
: $model;
2325
}
@@ -30,7 +32,13 @@ public function getRelation(string $name): Relation {
3032
$type = $class->getMethod($name)->getReturnType();
3133

3234
if ($type instanceof ReflectionNamedType && is_a($type->getName(), Relation::class, true)) {
33-
$relation = $this->model->newInstance()->{$name}();
35+
if ($this->builder) {
36+
$relation = Relation::noConstraints(function () use ($name) {
37+
return $this->model->newModelInstance()->{$name}();
38+
});
39+
} else {
40+
$relation = $this->model->{$name}();
41+
}
3442
}
3543
} catch (ReflectionException) {
3644
$relation = null;

0 commit comments

Comments
 (0)