3
3
namespace Reedware \LaravelRelationJoins ;
4
4
5
5
use Illuminate \Database \Eloquent \Builder ;
6
+ use Illuminate \Database \Eloquent \Model ;
6
7
use Illuminate \Database \Eloquent \Relations \BelongsTo ;
7
8
use Illuminate \Database \Eloquent \Relations \BelongsToMany ;
8
9
use Illuminate \Database \Eloquent \Relations \HasMany ;
16
17
use Illuminate \Database \Eloquent \Relations \Relation ;
17
18
use InvalidArgumentException ;
18
19
20
+ /**
21
+ * @template TRelatedModel of Model
22
+ * @template TDeclaringModel of Model
23
+ * @template TIntermediateModel of Model
24
+ * @template TResult
25
+ *
26
+ * @phpstan-type TRelation Relation<TRelatedModel,TDeclaringModel,TResult>
27
+ * @phpstan-type TBelongsTo BelongsTo<TRelatedModel,TDeclaringModel>
28
+ * @phpstan-type TBelongsToMany BelongsToMany<TRelatedModel,TDeclaringModel>
29
+ * @phpstan-type THasOne HasOne<TRelatedModel,TDeclaringModel>
30
+ * @phpstan-type THasMany HasMany<TRelatedModel,TDeclaringModel>
31
+ * @phpstan-type TMorphOne MorphOne<TRelatedModel,TDeclaringModel>
32
+ * @phpstan-type TMorphMany MorphMany<TRelatedModel,TDeclaringModel>
33
+ * @phpstan-type THasOneThrough HasOneThrough<TRelatedModel,TIntermediateModel,TDeclaringModel>
34
+ * @phpstan-type THasManyThrough HasManyThrough<TRelatedModel,TIntermediateModel,TDeclaringModel>
35
+ * @phpstan-type TMorphToMany MorphToMany<TRelatedModel,TDeclaringModel>
36
+ */
19
37
class RelationJoinQuery
20
38
{
21
39
/**
22
40
* Adds the constraints for a relationship join.
41
+ *
42
+ * @param TRelation $relation
43
+ * @param Builder<TRelatedModel> $query
44
+ * @param Builder<TDeclaringModel> $parentQuery
45
+ * @return Builder<TRelatedModel>
23
46
*/
24
47
public static function get (
25
48
Relation $ relation ,
@@ -53,6 +76,11 @@ public static function get(
53
76
54
77
/**
55
78
* Adds the constraints for a belongs to relationship join.
79
+ *
80
+ * @param TBelongsTo $relation
81
+ * @param Builder<TRelatedModel> $query
82
+ * @param Builder<TDeclaringModel> $parentQuery
83
+ * @return Builder<TRelatedModel>
56
84
*/
57
85
protected static function belongsTo (
58
86
BelongsTo $ relation ,
@@ -80,6 +108,11 @@ protected static function belongsTo(
80
108
81
109
/**
82
110
* Adds the constraints for a belongs to many relationship join.
111
+ *
112
+ * @param TBelongsToMany $relation
113
+ * @param Builder<TRelatedModel> $query
114
+ * @param Builder<TDeclaringModel> $parentQuery
115
+ * @return Builder<TRelatedModel>
83
116
*/
84
117
protected static function belongsToMany (
85
118
BelongsToMany $ relation ,
@@ -122,7 +155,7 @@ protected static function belongsToMany(
122
155
123
156
if (($ using = $ relation ->getPivotClass ()) != Pivot::class) {
124
157
$ query ->getQuery ()->joins [0 ] = new EloquentJoinClause (
125
- $ query ->getQuery ()->joins [0 ],
158
+ $ query ->getQuery ()->joins [0 ], // @phpstan-ignore offsetAccess.notFound (Join is added above)
126
159
(new $ using )->setTable ($ on )
127
160
);
128
161
}
@@ -138,6 +171,11 @@ protected static function belongsToMany(
138
171
139
172
/**
140
173
* Adds the constraints for a has one or has many relationship join.
174
+ *
175
+ * @param THasOne|THasMany|TMorphOne|TMorphMany $relation
176
+ * @param Builder<TRelatedModel> $query
177
+ * @param Builder<TDeclaringModel> $parentQuery
178
+ * @return Builder<TRelatedModel>
141
179
*/
142
180
protected static function hasOneOrMany (
143
181
HasOne |HasMany |MorphOne |MorphMany $ relation ,
@@ -166,11 +204,10 @@ protected static function hasOneOrMany(
166
204
/**
167
205
* Adds the constraints for a has one through or has many through relationship join.
168
206
*
169
- * Soft deletes on the parent model are not handled correctly until 7.10.0.
170
- * Most of the functionality works as expected otherwise. Given that 6.x
171
- * is nearing EoL, and 7.x is already EoL, we'll let it slide for now.
172
- *
173
- * @see https://github.com/laravel/framework/commit/de4c42f04d609b119a4e0a7e6223c37bfe54cb87
207
+ * @param THasOneThrough|THasManyThrough $relation
208
+ * @param Builder<TRelatedModel> $query
209
+ * @param Builder<TDeclaringModel> $parentQuery
210
+ * @return Builder<TRelatedModel>
174
211
*/
175
212
protected static function hasOneOrManyThrough (
176
213
HasOneThrough |HasManyThrough $ relation ,
@@ -220,7 +257,7 @@ protected static function hasOneOrManyThrough(
220
257
// scopes, we are going to define the query through eloquent instead.
221
258
222
259
$ query ->getQuery ()->joins [0 ] = new EloquentJoinClause (
223
- $ query ->getQuery ()->joins [0 ],
260
+ $ query ->getQuery ()->joins [0 ], // @phpstan-ignore offsetAccess.notFound (Join added above)
224
261
$ relation ->getParent ()->newInstance ()->setTable ($ on )
225
262
);
226
263
@@ -233,6 +270,11 @@ protected static function hasOneOrManyThrough(
233
270
234
271
/**
235
272
* Adds the constraints for a morph one or morph many relationship join.
273
+ *
274
+ * @param TMorphOne|TMorphMany $relation
275
+ * @param Builder<TRelatedModel> $query
276
+ * @param Builder<TDeclaringModel> $parentQuery
277
+ * @return Builder<TRelatedModel>
236
278
*/
237
279
protected static function morphOneOrMany (
238
280
MorphOne |MorphMany $ relation ,
@@ -254,6 +296,11 @@ protected static function morphOneOrMany(
254
296
255
297
/**
256
298
* Adds the constraints for a morph to many relationship join.
299
+ *
300
+ * @param TMorphToMany $relation
301
+ * @param Builder<TRelatedModel> $query
302
+ * @param Builder<TDeclaringModel> $parentQuery
303
+ * @return Builder<TRelatedModel>
257
304
*/
258
305
protected static function morphToMany (
259
306
MorphToMany $ relation ,
@@ -298,7 +345,7 @@ protected static function morphToMany(
298
345
299
346
if (($ using = $ relation ->getPivotClass ()) != Pivot::class) {
300
347
$ query ->getQuery ()->joins [0 ] = new EloquentJoinClause (
301
- $ query ->getQuery ()->joins [0 ],
348
+ $ query ->getQuery ()->joins [0 ], // @phpstan-ignore offsetAccess.notFound (Join added above)
302
349
(new $ using )->setTable ($ on )
303
350
);
304
351
}
0 commit comments