Skip to content

Commit 7b33ad3

Browse files
committed
Add annotations to query building classes
In order to help with auto-completion when users build queries from our document models.
1 parent aad17bb commit 7b33ad3

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

src/Eloquent/Builder.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
/**
3232
* @method \MongoDB\Laravel\Query\Builder toBase()
3333
* @template TModel of Model
34+
* @extends EloquentBuilder<TModel>
35+
*
36+
* @mixin \MongoDB\Laravel\Query\Builder
3437
*/
3538
class Builder extends EloquentBuilder
3639
{
@@ -69,7 +72,7 @@ class Builder extends EloquentBuilder
6972
];
7073

7174
/**
72-
* @return ($function is null ? AggregationBuilder : self)
75+
* @return ($function is null ? AggregationBuilder : $this)
7376
*
7477
* @inheritdoc
7578
*/

src/Eloquent/DocumentModel.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@
5050
use function strlen;
5151
use function var_export;
5252

53+
/**
54+
* @method static QueryBuilder query()
55+
* @method static self sole(array $columns)
56+
* @method static bool truncate()
57+
* @method static QueryBuilder where(Closure|string|array|Expression $column, mixed $operator = null, mixed $value = null, string $boolean = 'and')
58+
* @method static QueryBuilder whereBetween(string $column, iterable $values, string $boolean = null, bool $not = null)
59+
* @method static QueryBuilder whereIn(...$parameters, mixed $values, string $boolean = null, bool $not = null)
60+
* @method static QueryBuilder whereNot(array|Closure|Expression|string $column, $operator = null, $value = null, $boolean = 'and')
61+
* @method static QueryBuilder whereNotIn(...$parameters)
62+
* @method static QueryBuilder whereNotNull(...$parameters)
63+
* @method static QueryBuilder whereNull(string $value)
64+
* @mixin Eloquent
65+
*/
5366
trait DocumentModel
5467
{
5568
use HybridRelations;
@@ -553,8 +566,12 @@ public function getParentRelation(): ?Relation
553566
return $this->parentRelation ?? null;
554567
}
555568

556-
/** @inheritdoc */
557-
public function newEloquentBuilder($query)
569+
/**
570+
* @param $query
571+
* @return Builder
572+
*/
573+
#[Override]
574+
public function newEloquentBuilder($query): Builder
558575
{
559576
return new Builder($query);
560577
}
@@ -566,7 +583,7 @@ public function qualifyColumn($column)
566583
}
567584

568585
/** @inheritdoc */
569-
protected function newBaseQueryBuilder()
586+
protected function newBaseQueryBuilder(): QueryBuilder
570587
{
571588
$connection = $this->getConnection();
572589

tests/Models/Anniversary.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,11 @@
77
use Illuminate\Database\Eloquent\Model;
88
use MongoDB\Laravel\Eloquent\DocumentModel;
99
use MongoDB\Laravel\Eloquent\Model as Eloquent;
10-
use MongoDB\Laravel\Query\Builder;
1110

1211
/**
1312
* @property string $name
1413
* @property string $anniversary
1514
* @mixin Eloquent
16-
* @method static Builder create(...$values)
17-
* @method static Builder truncate()
18-
* @method static Eloquent sole(...$parameters)
1915
*/
2016
class Anniversary extends Model
2117
{

tests/Models/HiddenAnimal.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,11 @@
66

77
use Illuminate\Database\Eloquent\Model;
88
use MongoDB\Laravel\Eloquent\DocumentModel;
9-
use MongoDB\Laravel\Eloquent\Model as Eloquent;
10-
use MongoDB\Laravel\Query\Builder;
119

1210
/**
1311
* @property string $name
1412
* @property string $country
1513
* @property bool $can_be_eaten
16-
* @mixin Eloquent
17-
* @method static Builder create(...$values)
18-
* @method static Builder truncate()
19-
* @method static Eloquent sole(...$parameters)
2014
*/
2115
final class HiddenAnimal extends Model
2216
{

0 commit comments

Comments
 (0)