Skip to content
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

New Filter belongs to #975

Merged
merged 5 commits into from
Dec 23, 2024
Merged

New Filter belongs to #975

merged 5 commits into from
Dec 23, 2024

Conversation

gpibarra
Copy link
Contributor

@gpibarra gpibarra commented Oct 7, 2024

This new filter receives the id and allows to call the native method whereBelongsTo method of the model (doc).
The method accepts nested relationships.
The filter accepts receiving an array with the ids.

Unfortunately my English is not good and I couldn't write the documentation correctly.
If you think the PR is correct, could you help me with the documentation?

Copy link
Member

@AlexVanderbist AlexVanderbist left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, thanks for the PR! Two small review comments, after that I'm happy to tweak the docs and merge this :)

return $relatedModel;
}

protected function getModelFromRelation(Model $model, string $relation, int $level = 0): ?Model
Copy link
Member

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.

Copy link
Contributor Author

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.

{
public static function make($property): static
{
return new static("Filter property `{$property}` is invalid.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a $reason to this exception? From the implementation I see there's only one reason this happens: belongs to relationship is not an Eloquent relationship.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly.
There was another exception that I wasn't handling (on purpose), when the relation (or the last relation in a nested dotted string) is not a BelongTo relation, in that case an unhandled exception was thrown but Laravel gave a correct message and context to find this error. This case was not covered in the tests.
Based on that exception, now remove InvalidFilterProperty and throw that same exception RelationNotFoundException. I also added more test cases that cover all the options (this case and the previous one).
This native Laravel exception gives a more specific message to fix the error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AlexVanderbist Have you been able to review this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@freekmurze Have you been able to review this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect, thank you!

@AlexVanderbist AlexVanderbist merged commit 0348ab0 into spatie:main Dec 23, 2024
1 check passed
@gpibarra
Copy link
Contributor Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants