@@ -115,23 +115,31 @@ public function getWhereIn(string $column, $values)
115
115
* @param string|array $column
116
116
* @param mixed $value
117
117
*
118
- * @return Builder| Model|object|null
118
+ * @return \Illuminate\Database\Eloquent\ Model|object|null
119
119
*/
120
120
public function getWhereFirst ($ column , $ value = null )
121
121
{
122
- if (is_array ($ column )) {
123
- $ model = $ this ->model ->where ($ column )->first ();
124
- } else {
125
- $ model = $ this ->model ->where ($ column , $ value )->first ();
126
- }
127
-
122
+ $ model = $ this ->buildGetWhereDelegator ($ column , $ value )->first ();
128
123
if (!$ model ) {
129
124
$ this ->throwModelNotFoundException ();
130
125
}
131
-
132
126
return $ model ;
133
127
}
134
128
129
+ /**
130
+ * Build delegator for get where expression
131
+ *
132
+ * @param $column
133
+ * @param null $value
134
+ *
135
+ * @return \Illuminate\Database\Eloquent\Builder
136
+ */
137
+ private function buildGetWhereDelegator ($ column , $ value = null ): Builder
138
+ {
139
+ return is_array ($ column ) ?
140
+ $ this ->model ->where ($ column ) : $ this ->model ->where ($ column , $ value );
141
+ }
142
+
135
143
/**
136
144
* Finds first model with "whereIn" condition.
137
145
*
@@ -161,7 +169,7 @@ public function getWhereInFirst(string $column, $values)
161
169
*/
162
170
public function count ($ column , $ value = null )
163
171
{
164
- return $ this ->getWhere ($ column , $ value )->count ();
172
+ return $ this ->buildGetWhereDelegator ($ column , $ value )->count ();
165
173
}
166
174
167
175
/**
@@ -174,11 +182,7 @@ public function count($column, $value = null)
174
182
*/
175
183
public function getWhere ($ column , $ value = null )
176
184
{
177
- if (is_array ($ column )) {
178
- return $ this ->model ->where ($ column )->get ();
179
- }
180
-
181
- return $ this ->model ->where ($ column , $ value )->get ();
185
+ return $ this ->buildGetWhereDelegator ($ column , $ value )->get ();
182
186
}
183
187
184
188
/**
@@ -191,7 +195,7 @@ public function getWhere($column, $value = null)
191
195
*/
192
196
public function existed ($ column , $ value = null )
193
197
{
194
- return $ this ->getWhere ($ column , $ value )->count () > 0 ;
198
+ return $ this ->buildGetWhereDelegator ($ column , $ value )->exists () ;
195
199
}
196
200
197
201
/**
@@ -206,11 +210,19 @@ public function existed($column, $value = null)
206
210
*/
207
211
public function getWhereWithSorted (array $ conditions , array $ sortedBy = null , ?int $ limit = 5000 , ?int $ offset = 0 )
208
212
{
209
- $ delegator = $ this ->getDelegatorByConditionsAndSortedBy ($ conditions , $ sortedBy );
213
+ $ delegator = $ this ->buildGetWhereDelegatorWithOrderBy ($ conditions , $ sortedBy );
210
214
return $ delegator ->skip ($ offset )->take ($ limit )->get ();
211
215
}
212
216
213
- private function getDelegatorByConditionsAndSortedBy (array $ conditions , array $ sortedBy = null )
217
+ /**
218
+ * Build delegator get where expression with sorted by
219
+ *
220
+ * @param array $conditions
221
+ * @param array|null $sortedBy
222
+ *
223
+ * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model
224
+ */
225
+ private function buildGetWhereDelegatorWithOrderBy (array $ conditions , array $ sortedBy = null )
214
226
{
215
227
$ sortedBy = empty ($ sortedBy ) ? [$ this ->model ->getKeyName () => 'desc ' ] : $ sortedBy ;
216
228
$ delegator = $ this ->model ->where ($ conditions );
@@ -230,8 +242,8 @@ private function getDelegatorByConditionsAndSortedBy(array $conditions, array $s
230
242
*/
231
243
public function getWhereFirstWithSorted (array $ conditions , array $ sortedBy = null )
232
244
{
233
- $ delegator = $ this ->getDelegatorByConditionsAndSortedBy ($ conditions , $ sortedBy );
234
- $ model = $ delegator ->take ( 1 )-> first ();
245
+ $ delegator = $ this ->buildGetWhereDelegatorWithOrderBy ($ conditions , $ sortedBy );
246
+ $ model = $ delegator ->first ();
235
247
if (!$ model ) {
236
248
$ this ->throwModelNotFoundException ();
237
249
}
0 commit comments