-
-
Notifications
You must be signed in to change notification settings - Fork 403
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* new filter BelongsTo * test new filter BelongsTo * wip doc new filter BelongsTo * Fix styling * change RelationNotFoundException instead of InvalidFilterProperty, add tests --------- Co-authored-by: gpibarra <[email protected]>
- Loading branch information
Showing
4 changed files
with
223 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
namespace Spatie\QueryBuilder\Filters; | ||
|
||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\RelationNotFoundException; | ||
use Illuminate\Database\Eloquent\Relations\Relation; | ||
use Illuminate\Support\Arr; | ||
|
||
/** | ||
* @template TModelClass of \Illuminate\Database\Eloquent\Model | ||
* @template-implements \Spatie\QueryBuilder\Filters\Filter<TModelClass> | ||
*/ | ||
class FiltersBelongsTo implements Filter | ||
{ | ||
/** {@inheritdoc} */ | ||
public function __invoke(Builder $query, $value, string $property) | ||
{ | ||
$values = array_values(Arr::wrap($value)); | ||
|
||
$propertyParts = collect(explode('.', $property)); | ||
$relation = $propertyParts->pop(); | ||
$relationParent = $propertyParts->implode('.'); | ||
$relatedModel = $this->getRelatedModel($query->getModel(), $relation, $relationParent); | ||
|
||
$relatedCollection = $relatedModel->newCollection(); | ||
array_walk($values, fn ($v) => $relatedCollection->add( | ||
tap($relatedModel->newInstance(), fn ($m) => $m->setAttribute($m->getKeyName(), $v)) | ||
)); | ||
|
||
if ($relatedCollection->isEmpty()) { | ||
return $query; | ||
} | ||
|
||
if ($relationParent) { | ||
$query->whereHas($relationParent, fn (Builder $q) => $q->whereBelongsTo($relatedCollection, $relation)); | ||
} else { | ||
$query->whereBelongsTo($relatedCollection, $relation); | ||
} | ||
} | ||
|
||
protected function getRelatedModel(Model $modelQuery, string $relationName, string $relationParent): Model | ||
{ | ||
if ($relationParent) { | ||
$modelParent = $this->getModelFromRelation($modelQuery, $relationParent); | ||
} else { | ||
$modelParent = $modelQuery; | ||
} | ||
|
||
$relatedModel = $this->getRelatedModelFromRelation($modelParent, $relationName); | ||
|
||
return $relatedModel; | ||
} | ||
|
||
protected function getRelatedModelFromRelation(Model $model, string $relationName): ?Model | ||
{ | ||
$relationObject = $model->$relationName(); | ||
if (! is_subclass_of($relationObject, Relation::class)) { | ||
throw RelationNotFoundException::make($model, $relationName); | ||
} | ||
|
||
$relatedModel = $relationObject->getRelated(); | ||
|
||
return $relatedModel; | ||
} | ||
|
||
protected function getModelFromRelation(Model $model, string $relation, int $level = 0): ?Model | ||
{ | ||
$relationParts = explode('.', $relation); | ||
if (count($relationParts) == 1) { | ||
return $this->getRelatedModelFromRelation($model, $relation); | ||
} else { | ||
$firstRelation = $relationParts[0]; | ||
$firstRelatedModel = $this->getRelatedModelFromRelation($model, $firstRelation); | ||
if (! $firstRelatedModel) { | ||
return null; | ||
} | ||
|
||
return $this->getModelFromRelation($firstRelatedModel, implode('.', array_slice($relationParts, 1)), $level + 1); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters