|
2 | 2 |
|
3 | 3 | namespace Alexmg86\LaravelSubQuery;
|
4 | 4 |
|
| 5 | +use Closure; |
5 | 6 | use Illuminate\Database\Eloquent\Builder;
|
6 | 7 | use Illuminate\Database\Eloquent\Concerns\QueriesRelationships;
|
| 8 | +use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
| 9 | +use Illuminate\Database\Eloquent\Relations\HasMany; |
| 10 | +use Illuminate\Database\Eloquent\Relations\HasManyThrough; |
7 | 11 | use Illuminate\Database\Query\Expression;
|
8 | 12 | use Illuminate\Support\Str;
|
9 | 13 |
|
@@ -208,4 +212,80 @@ public function setWithAvg($withAvg)
|
208 | 212 | {
|
209 | 213 | return $this->withAvg($withAvg);
|
210 | 214 | }
|
| 215 | + |
| 216 | + /** |
| 217 | + * Eager load the relationships for the models. |
| 218 | + * Overwriting Illuminate\Database\Eloquent\Builder@eagerLoadRelations |
| 219 | + * |
| 220 | + * @param array $models |
| 221 | + * @return array |
| 222 | + */ |
| 223 | + public function eagerLoadRelationsOne(array $models, string $type) |
| 224 | + { |
| 225 | + foreach ($this->eagerLoad as $name => $constraints) { |
| 226 | + if (strpos($name, '.') === false) { |
| 227 | + $models = $this->eagerLoadRelationOne($models, $name, $constraints, $type); |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + return $models; |
| 232 | + } |
| 233 | + |
| 234 | + /** |
| 235 | + * Eagerly load the relationship on a set of models. |
| 236 | + * Overwriting Illuminate\Database\Eloquent\Builder@eagerLoadRelation |
| 237 | + * |
| 238 | + * @param array $models |
| 239 | + * @param string $name |
| 240 | + * @param \Closure $constraints |
| 241 | + * @return array |
| 242 | + */ |
| 243 | + protected function eagerLoadRelationOne(array $models, $name, Closure $constraints, string $type) |
| 244 | + { |
| 245 | + $relation = $this->getRelation($name); |
| 246 | + |
| 247 | + $relation->addEagerConstraints($models); |
| 248 | + |
| 249 | + $constraints($relation); |
| 250 | + |
| 251 | + $parseData = $this->parseRelationToKeys($relation, $type); |
| 252 | + |
| 253 | + return $relation->match( |
| 254 | + $relation->initRelation($models, $name), |
| 255 | + $relation |
| 256 | + ->whereIn($parseData[0], $parseData[1]) |
| 257 | + ->getEager(), |
| 258 | + $name |
| 259 | + ); |
| 260 | + } |
| 261 | + |
| 262 | + /** |
| 263 | + * Getting only need ids |
| 264 | + * @param object $relation |
| 265 | + * @param string $type |
| 266 | + * @return array |
| 267 | + */ |
| 268 | + private function parseRelationToKeys(object $relation, string $type) |
| 269 | + { |
| 270 | + $relationCopy = clone $relation; |
| 271 | + |
| 272 | + $typeRelation = get_class($relationCopy); |
| 273 | + switch (get_class($relationCopy)) { |
| 274 | + case HasMany::class: |
| 275 | + $maxKey = $asKey = $relation->getLocalKeyName(); |
| 276 | + $groupby = $relation->getForeignKeyName(); |
| 277 | + break; |
| 278 | + case BelongsToMany::class: |
| 279 | + $maxKey = $asKey = $relation->getRelatedKeyName(); |
| 280 | + $groupby = $relation->getRelated()->getTable() . '.' . $relation->getForeignPivotKeyName(); |
| 281 | + break; |
| 282 | + case HasManyThrough::class: |
| 283 | + $asKey = $relation->getSecondLocalKeyName(); |
| 284 | + $maxKey = $relation->getRelated()->getTable() . '.' . $asKey; |
| 285 | + $groupby = $relation->getParent()->getTable() . '.' . $relation->getFirstKeyName(); |
| 286 | + break; |
| 287 | + } |
| 288 | + |
| 289 | + return [$maxKey, $relationCopy->selectRaw("$type($maxKey) as $asKey")->groupby($groupby)->pluck($asKey)]; |
| 290 | + } |
211 | 291 | }
|
0 commit comments