Skip to content

Conditional relationships #6

Closed
Closed
@lindyhopchris

Description

@lindyhopchris

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions