Skip to content
Discussion options

You must be logged in to vote

Include pivot data in your resource attributes, not in the relationship definition.

Model Setup

public function roles()
{
    return $this->belongsToMany(Role::class)->withPivot('assigned_at');
}

Resource Implementation

class RoleResource extends JsonApiResource
{
    public function toAttributes(): array
    {
        return [
            'name' => $this->resource->name,

            'assignedAt' => $this->when(
                isset($this->resource->pivot),
                $this->resource->pivot?->assigned_at
            ),
        ];
    }

    public function toRelationships(): array
    {
        return [];
    }
}

User Resource

class UserResource extends JsonApiResource
{
    public f…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@SantosVilanculos
Comment options

@macropay-solutions
Comment options

Answer selected by SantosVilanculos
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants