-
-
Notifications
You must be signed in to change notification settings - Fork 404
New Filter belongs to #975
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0ec10e8
new filter BelongsTo
gpibarra 73498b0
test new filter BelongsTo
gpibarra 220d6aa
wip doc new filter BelongsTo
gpibarra 4ce4259
Fix styling
gpibarra 4b3f57e
change RelationNotFoundException instead of InvalidFilterProperty, ad…
gpibarra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
$level
still being used? I can see it's passed on recursively but never used.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a habit, whenever I make a recursive function I send the level parameter, sometimes it is necessary to determine the first iteration of the rest. In this case it was not necessary and I forgot to delete it. You can delete it safely.