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

Fix alias for allowed includes #414

Merged
merged 1 commit into from
Feb 11, 2020
Merged

Conversation

dominikb
Copy link
Contributor

Addresses issue #412

@Sharptsa
Copy link

@dominikb Thanks for taking the time !

I tried this on my codebase, works great with simple relationship and counts but fails with nested relationship.

AllowedInclude::relationship( 'relationshipAlias', 'relatedModel.nestedRelatedModel');

Produces this error :

Argument 1 passed to Spatie\\QueryBuilder\\AllowedInclude::__construct() must be of the type string, null given, called in /var/www/back/vendor/spatie/laravel-query-builder/src/AllowedInclude.php on line 38 at /var/www/back/vendor/spatie/laravel-query-builder/src/AllowedInclude.php:22)"

Though, now that I'm thinking about it, I don't know what is the intended behavior aliasing a nested relationship.
And what expected includes it should produce.

  • relatedModel and relationshipAlias ?
  • just relationshipAlias and ignore the traversing relationship ?
  • something else ?

@dominikb
Copy link
Contributor Author

Hey, thanks for testing my PR.

I, maybe wrongfully, assumed that an aliased relationship would also include the previous model name:

// This should work
AllowedInclude::relationship('firstModelAlias.secondModelAlias', 'firstModel.secondModel')

// but this would not
AllowedInclude::relationship('secondModelAlias', 'firstModel.secondModel')

The logic could be extended to handle both cases, by determining if the alias has a dot in its name.

I'll update the PR shortly. The maintainers then need to make a decision which behavior we want to support.

@Sharptsa
Copy link

Ah ok, I tested

AllowedInclude::relationship('firstModelAlias.secondModelAlias', 'firstModel.secondModel')

And it works fine indeed. 👍

I was hoping this fix would also allow me to alias deep nested relationships to simplify the includes I have to pass in my requests.
But I guess this is a bit out of the scope of the issue I reported. This would be more suited as a feature/enhancement than a bugfix.

Would be great to see what the maintainers think.

Maybe for now we should just throw a proper exception if the depth of the alias and nested relationship don't match.

@AlexVanderbist
Copy link
Member

Thanks you both for looking into this. I'm gonna merge this first, just to fix the issue at hand.

Afterwards, feel free to open a new issue to discuss aliasing nested relationships. As a start to that discussion: personally I've never felt the need for hiding nested relationships as the response is always gonna reflect that nested structure. I can see how it's useful for more complex API's too tho.

@AlexVanderbist AlexVanderbist merged commit 44aee49 into spatie:master Feb 11, 2020
@dominikb dominikb deleted the fix-issue-412 branch October 6, 2020 06:55
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.

3 participants