|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <dunglas@gmail.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Laravel\Eloquent\Filter; |
| 15 | + |
| 16 | +use ApiPlatform\Metadata\Parameter; |
| 17 | +use Illuminate\Database\Eloquent\Builder; |
| 18 | + |
| 19 | +/** |
| 20 | + * @internal |
| 21 | + */ |
| 22 | +trait NestedPropertyTrait |
| 23 | +{ |
| 24 | + /** |
| 25 | + * Applies a filter condition supporting nested properties via relationships. |
| 26 | + * |
| 27 | + * @param Builder<\Illuminate\Database\Eloquent\Model> $builder |
| 28 | + * @param callable $condition Callback receiving ($query, $property) to apply the actual filter condition |
| 29 | + * |
| 30 | + * @return Builder<\Illuminate\Database\Eloquent\Model> |
| 31 | + */ |
| 32 | + private function applyWithNestedProperty( |
| 33 | + Builder $builder, |
| 34 | + Parameter $parameter, |
| 35 | + callable $condition, |
| 36 | + string $whereClause = 'where', |
| 37 | + ): Builder { |
| 38 | + $nestedInfo = $parameter->getExtraProperties()['nested_property_info'] ?? null; |
| 39 | + |
| 40 | + if (!$nestedInfo) { |
| 41 | + // No nested property, use simple where clause |
| 42 | + $property = $this->getQueryProperty($parameter); |
| 43 | + |
| 44 | + return $condition($builder, $property, $whereClause); |
| 45 | + } |
| 46 | + |
| 47 | + // Handle nested property using whereHas |
| 48 | + // For Laravel Eloquent, use the original relation names (camelCase method names) |
| 49 | + // not the converted names (snake_case database columns) |
| 50 | + $relationSegments = $nestedInfo['relation_segments']; |
| 51 | + $leafProperty = $nestedInfo['leaf_property']; |
| 52 | + |
| 53 | + if (0 === \count($relationSegments)) { |
| 54 | + // Edge case: no relations, just a property |
| 55 | + return $condition($builder, $leafProperty, $whereClause); |
| 56 | + } |
| 57 | + |
| 58 | + // Build nested whereHas callbacks from innermost to outermost |
| 59 | + // For product.name: whereHas('product', fn($q) => $q->where('name', ...)) |
| 60 | + // For product.variations.code: whereHas('product', fn($q) => $q->whereHas('variations', fn($q2) => $q2->where('code', ...))) |
| 61 | + |
| 62 | + $callback = static function ($query) use ($leafProperty, $condition, $whereClause) { |
| 63 | + return $condition($query, $leafProperty, $whereClause); |
| 64 | + }; |
| 65 | + |
| 66 | + // Build the chain from the end to the beginning |
| 67 | + for ($i = \count($relationSegments) - 1; $i > 0; --$i) { |
| 68 | + $relation = $relationSegments[$i]; |
| 69 | + $previousCallback = $callback; |
| 70 | + $callback = static function ($query) use ($relation, $previousCallback) { |
| 71 | + return $query->whereHas($relation, $previousCallback); |
| 72 | + }; |
| 73 | + } |
| 74 | + |
| 75 | + // Apply the outermost whereHas |
| 76 | + return $builder->whereHas($relationSegments[0], $callback); |
| 77 | + } |
| 78 | +} |
0 commit comments