8
8
use Illuminate \Database \Eloquent \Relations \BelongsTo ;
9
9
use Illuminate \Database \Eloquent \Relations \BelongsToMany ;
10
10
use Illuminate \Database \Eloquent \Relations \HasOneOrMany ;
11
+ use Illuminate \Database \Eloquent \Relations \Relation ;
11
12
use Jenssegers \Mongodb \Eloquent \Model ;
12
13
13
14
trait QueriesRelationships
14
15
{
15
16
/**
16
17
* Add a relationship count / exists condition to the query.
17
- * @param string $relation
18
+ * @param Relation| string $relation
18
19
* @param string $operator
19
20
* @param int $count
20
21
* @param string $boolean
@@ -23,11 +24,13 @@ trait QueriesRelationships
23
24
*/
24
25
public function has ($ relation , $ operator = '>= ' , $ count = 1 , $ boolean = 'and ' , Closure $ callback = null )
25
26
{
26
- if (strpos ($ relation , '. ' ) !== false ) {
27
- return $ this ->hasNested ($ relation , $ operator , $ count , $ boolean , $ callback );
28
- }
27
+ if (is_string ($ relation )) {
28
+ if (strpos ($ relation , '. ' ) !== false ) {
29
+ return $ this ->hasNested ($ relation , $ operator , $ count , $ boolean , $ callback );
30
+ }
29
31
30
- $ relation = $ this ->getRelationWithoutConstraints ($ relation );
32
+ $ relation = $ this ->getRelationWithoutConstraints ($ relation );
33
+ }
31
34
32
35
// If this is a hybrid relation then we can not use a normal whereExists() query that relies on a subquery
33
36
// We need to use a `whereIn` query
@@ -59,25 +62,25 @@ public function has($relation, $operator = '>=', $count = 1, $boolean = 'and', C
59
62
}
60
63
61
64
/**
62
- * @param $relation
65
+ * @param Relation $relation
63
66
* @return bool
64
67
*/
65
- protected function isAcrossConnections ($ relation )
68
+ protected function isAcrossConnections (Relation $ relation )
66
69
{
67
70
return $ relation ->getParent ()->getConnectionName () !== $ relation ->getRelated ()->getConnectionName ();
68
71
}
69
72
70
73
/**
71
74
* Compare across databases
72
- * @param $relation
75
+ * @param Relation $relation
73
76
* @param string $operator
74
77
* @param int $count
75
78
* @param string $boolean
76
79
* @param Closure|null $callback
77
80
* @return mixed
78
81
* @throws Exception
79
82
*/
80
- public function addHybridHas ($ relation , $ operator = '>= ' , $ count = 1 , $ boolean = 'and ' , Closure $ callback = null )
83
+ public function addHybridHas (Relation $ relation , $ operator = '>= ' , $ count = 1 , $ boolean = 'and ' , Closure $ callback = null )
81
84
{
82
85
$ hasQuery = $ relation ->getQuery ();
83
86
if ($ callback ) {
@@ -99,10 +102,10 @@ public function addHybridHas($relation, $operator = '>=', $count = 1, $boolean =
99
102
}
100
103
101
104
/**
102
- * @param $relation
105
+ * @param Relation $relation
103
106
* @return string
104
107
*/
105
- protected function getHasCompareKey ($ relation )
108
+ protected function getHasCompareKey (Relation $ relation )
106
109
{
107
110
if (method_exists ($ relation , 'getHasCompareKey ' )) {
108
111
return $ relation ->getHasCompareKey ();
@@ -147,14 +150,14 @@ protected function getConstrainedRelatedIds($relations, $operator, $count)
147
150
148
151
/**
149
152
* Returns key we are constraining this parent model's query with
150
- * @param $relation
153
+ * @param Relation $relation
151
154
* @return string
152
155
* @throws Exception
153
156
*/
154
- protected function getRelatedConstraintKey ($ relation )
157
+ protected function getRelatedConstraintKey (Relation $ relation )
155
158
{
156
159
if ($ relation instanceof HasOneOrMany) {
157
- return $ this -> model -> getKeyName ();
160
+ return $ relation -> getLocalKeyName ();
158
161
}
159
162
160
163
if ($ relation instanceof BelongsTo) {
0 commit comments