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

Conditional relationships #6

Closed
lindyhopchris opened this issue Jan 31, 2021 · 0 comments
Closed

Conditional relationships #6

lindyhopchris opened this issue Jan 31, 2021 · 0 comments
Labels
enhancement New feature or request

Comments

@lindyhopchris
Copy link
Contributor

At the moment the resource objects support conditional attributes in the attributes and meta values. We should also add support for conditional relationships.

To do this, we need to support (in the encoder) having relations wrapped in either a conditional attribute or conditional attributes object. So the syntax on the resource would be:

public function relationships($request): iterable
{
    return [
        $this->relation('author'),
        $this->when($request->user()->isAdmin(), $this->relation('publishedBy')),
        $this->mergeWhen($request->user()->isOwner(), [
            $this->relation('foo'),
            $this->relation('bar'),
        ]),
    ];
}

Probably we should rename the ConditionalAttr and ConditionalAttrs classes to ConditionalField and ConditionalFields considering we're now not just using them in attributes.

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

No branches or pull requests

1 participant