| Conditions | 5 |
| Paths | 6 |
| Total Lines | 90 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 205 | private function compileRelationSearch($query, $relation, $column, $keyword) |
||
| 206 | { |
||
| 207 | $myQuery = clone $this->query; |
||
| 208 | |||
| 209 | /** |
||
| 210 | * For compile nested relation, we need store all nested relation as array |
||
| 211 | * and reverse order to apply where query. |
||
| 212 | * With this method we can create nested sub query with properly relation. |
||
| 213 | */ |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Store all relation data that require in next step |
||
| 217 | */ |
||
| 218 | $relationChunk = []; |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Store last eloquent query builder for get next relation. |
||
| 222 | */ |
||
| 223 | $lastQuery = $query; |
||
| 224 | |||
| 225 | $relations = explode('.', $relation); |
||
| 226 | $lastRelation = end($relations); |
||
| 227 | foreach ($relations as $relation) { |
||
| 228 | $relationType = $myQuery->getModel()->{$relation}(); |
||
| 229 | $myQuery->orWhereHas($relation, function ($builder) use ( |
||
| 230 | $column, |
||
| 231 | $keyword, |
||
| 232 | $query, |
||
| 233 | $relationType, |
||
| 234 | $relation, |
||
| 235 | $lastRelation, |
||
| 236 | &$relationChunk, |
||
| 237 | &$lastQuery |
||
| 238 | ) { |
||
| 239 | $builder->select($this->connection->raw('count(1)')); |
||
| 240 | |||
| 241 | // We will perform search on last relation only. |
||
| 242 | if ($relation == $lastRelation) { |
||
| 243 | $this->compileQuerySearch($builder, $column, $keyword, ''); |
||
| 244 | } |
||
| 245 | |||
| 246 | // Put require object to next step!! |
||
| 247 | $relationChunk[$relation] = [ |
||
| 248 | 'builder' => $builder, |
||
| 249 | 'relationType' => $relationType, |
||
| 250 | 'query' => $lastQuery, |
||
| 251 | ]; |
||
| 252 | |||
| 253 | // This is trick make sub query. |
||
| 254 | $lastQuery = $builder; |
||
| 255 | }); |
||
| 256 | |||
| 257 | // This is trick to make nested relation by pass previous relation to be next query eloquent builder |
||
| 258 | $myQuery = $relationType; |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Reverse them all |
||
| 263 | */ |
||
| 264 | $relationChunk = array_reverse($relationChunk, true); |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Create valuable for use in check last relation |
||
| 268 | */ |
||
| 269 | end($relationChunk); |
||
| 270 | $lastRelation = key($relationChunk); |
||
| 271 | reset($relationChunk); |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Walking ... |
||
| 275 | */ |
||
| 276 | foreach ($relationChunk as $relation => $chunk) { |
||
| 277 | // Prepare variables |
||
| 278 | $builder = $chunk['builder']; |
||
| 279 | $query = $chunk['query']; |
||
| 280 | $bindings = $builder->getBindings(); |
||
| 281 | $builder = "({$builder->toSql()}) >= 1"; |
||
| 282 | |||
| 283 | // Check if it last relation we will use orWhereRaw |
||
| 284 | if ($lastRelation == $relation) { |
||
| 285 | $relationMethod = "orWhereRaw"; |
||
| 286 | } else { |
||
| 287 | // For case parent relation of nested relation. |
||
| 288 | // We must use and for properly query and get correct result |
||
| 289 | $relationMethod = "whereRaw"; |
||
| 290 | } |
||
| 291 | |||
| 292 | $query->{$relationMethod}($builder, $bindings); |
||
| 293 | } |
||
| 294 | } |
||
| 295 | } |
||
| 296 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: