1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Recca0120\Repository\Concerns; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Recca0120\Repository\Method; |
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
8
|
|
|
|
9
|
|
|
trait QueriesRelationships |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Add a relationship count / exists condition to the query. |
13
|
|
|
* |
14
|
|
|
* @param string $relation |
15
|
|
|
* @param string $operator |
16
|
|
|
* @param int $count |
17
|
|
|
* @param string $boolean |
18
|
|
|
* @param \Closure|null $callback |
19
|
|
|
* @return $this |
20
|
|
|
*/ |
21
|
|
|
public function has($relation, $operator = '>=', $count = 1, $boolean = 'and', Closure $callback = null) |
22
|
|
|
{ |
23
|
|
|
$this->methods[] = new Method(__FUNCTION__, [$relation, $operator, $count, $boolean, $callback]); |
|
|
|
|
24
|
|
|
|
25
|
|
|
return $this; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Add a relationship count / exists condition to the query with an "or". |
30
|
|
|
* |
31
|
|
|
* @param string $relation |
32
|
|
|
* @param string $operator |
33
|
|
|
* @param int $count |
34
|
|
|
* @return $this |
35
|
|
|
*/ |
36
|
|
|
public function orHas($relation, $operator = '>=', $count = 1) |
37
|
|
|
{ |
38
|
|
|
$this->methods[] = new Method(__FUNCTION__, [$relation, $operator, $count]); |
39
|
|
|
|
40
|
|
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Add a relationship count / exists condition to the query. |
45
|
|
|
* |
46
|
|
|
* @param string $relation |
47
|
|
|
* @param string $boolean |
48
|
|
|
* @param \Closure|null $callback |
49
|
|
|
* @return $this |
50
|
|
|
*/ |
51
|
|
|
public function doesntHave($relation, $boolean = 'and', Closure $callback = null) |
52
|
|
|
{ |
53
|
|
|
$this->methods[] = new Method(__FUNCTION__, [$relation, $boolean, $callback]); |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Add a relationship count / exists condition to the query with where clauses. |
60
|
|
|
* |
61
|
|
|
* @param string $relation |
62
|
|
|
* @param \Closure|null $callback |
63
|
|
|
* @param string $operator |
64
|
|
|
* @param int $count |
65
|
|
|
* @return $this |
66
|
|
|
*/ |
67
|
|
|
public function whereHas($relation, Closure $callback = null, $operator = '>=', $count = 1) |
68
|
|
|
{ |
69
|
|
|
$this->methods[] = new Method(__FUNCTION__, [$relation, $callback, $operator, $count]); |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Add a relationship count / exists condition to the query with where clauses and an "or". |
76
|
|
|
* |
77
|
|
|
* @param string $relation |
78
|
|
|
* @param \Closure|null $callback |
79
|
|
|
* @param string $operator |
80
|
|
|
* @param int $count |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
|
|
public function orWhereHas($relation, Closure $callback = null, $operator = '>=', $count = 1) |
84
|
|
|
{ |
85
|
|
|
$this->methods[] = new Method(__FUNCTION__, [$relation, $callback, $operator, $count]); |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Add a relationship count / exists condition to the query with where clauses. |
92
|
|
|
* |
93
|
|
|
* @param string $relation |
94
|
|
|
* @param \Closure|null $callback |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
|
|
public function whereDoesntHave($relation, Closure $callback = null) |
98
|
|
|
{ |
99
|
|
|
$this->methods[] = new Method(__FUNCTION__, [$relation, $callback]); |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Add subselect queries to count the relations. |
106
|
|
|
* |
107
|
|
|
* @param mixed $relations |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
|
|
public function withCount($relations) |
111
|
|
|
{ |
112
|
|
|
$this->methods[] = new Method(__FUNCTION__, [$relations]); |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Merge the where constraints from another query to the current query. |
119
|
|
|
* |
120
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $from |
121
|
|
|
* @return $this |
122
|
|
|
*/ |
123
|
|
|
public function mergeConstraintsFrom(Builder $from) |
124
|
|
|
{ |
125
|
|
|
$this->methods[] = new Method(__FUNCTION__, [$from]); |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: