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 |
||
123 | private function compileRelationSearch($query, $relation, $column, $keyword) |
||
124 | { |
||
125 | $myQuery = clone $this->query; |
||
126 | |||
127 | /** |
||
128 | * For compile nested relation, we need store all nested relation as array |
||
129 | * and reverse order to apply where query. |
||
130 | * With this method we can create nested sub query with properly relation. |
||
131 | */ |
||
132 | |||
133 | /** |
||
134 | * Store all relation data that require in next step |
||
135 | */ |
||
136 | $relationChunk = []; |
||
137 | |||
138 | /** |
||
139 | * Store last eloquent query builder for get next relation. |
||
140 | */ |
||
141 | $lastQuery = $query; |
||
142 | |||
143 | $relations = explode('.', $relation); |
||
144 | $lastRelation = end($relations); |
||
145 | foreach ($relations as $relation) { |
||
146 | $relationType = $myQuery->getModel()->{$relation}(); |
||
147 | $myQuery->orWhereHas($relation, function ($builder) use ( |
||
148 | $column, |
||
149 | $keyword, |
||
150 | $query, |
||
151 | $relationType, |
||
152 | $relation, |
||
153 | $lastRelation, |
||
154 | &$relationChunk, |
||
155 | &$lastQuery |
||
156 | ) { |
||
157 | $builder->select($this->connection->raw('count(1)')); |
||
158 | |||
159 | // We will perform search on last relation only. |
||
160 | if ($relation == $lastRelation) { |
||
161 | $this->compileQuerySearch($builder, $column, $keyword, ''); |
||
162 | } |
||
163 | |||
164 | // Put require object to next step!! |
||
165 | $relationChunk[$relation] = [ |
||
166 | 'builder' => $builder, |
||
167 | 'relationType' => $relationType, |
||
168 | 'query' => $lastQuery, |
||
169 | ]; |
||
170 | |||
171 | // This is trick make sub query. |
||
172 | $lastQuery = $builder; |
||
173 | }); |
||
174 | |||
175 | // This is trick to make nested relation by pass previous relation to be next query eloquent builder |
||
176 | $myQuery = $relationType; |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Reverse them all |
||
181 | */ |
||
182 | $relationChunk = array_reverse($relationChunk, true); |
||
183 | |||
184 | /** |
||
185 | * Create valuable for use in check last relation |
||
186 | */ |
||
187 | end($relationChunk); |
||
188 | $lastRelation = key($relationChunk); |
||
189 | reset($relationChunk); |
||
190 | |||
191 | /** |
||
192 | * Walking ... |
||
193 | */ |
||
194 | foreach ($relationChunk as $relation => $chunk) { |
||
195 | /** @var Builder $builder */ |
||
196 | $builder = $chunk['builder']; |
||
197 | $query = $chunk['query']; |
||
198 | $bindings = $builder->getBindings(); |
||
199 | $builder = "({$builder->toSql()}) >= 1"; |
||
200 | |||
201 | // Check if it last relation we will use orWhereRaw |
||
202 | if ($lastRelation == $relation) { |
||
203 | $relationMethod = "orWhereRaw"; |
||
204 | } else { |
||
205 | // For case parent relation of nested relation. |
||
206 | // We must use and for properly query and get correct result |
||
207 | $relationMethod = "whereRaw"; |
||
208 | } |
||
209 | |||
210 | $query->{$relationMethod}($builder, $bindings); |
||
211 | } |
||
212 | } |
||
213 | |||
310 |