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

Why not read/print "Polymorphic To-Many Relations" #164

Closed
joserick opened this issue Feb 9, 2022 · 2 comments
Closed

Why not read/print "Polymorphic To-Many Relations" #164

joserick opened this issue Feb 9, 2022 · 2 comments

Comments

@joserick
Copy link

joserick commented Feb 9, 2022

Hi,

I have a problem with this relationship:

class Banner extends Model
{
    /**
     * Get all of the series for the banner.
     */
    public function series()
    {
        return $this->morphedByMany(Serie::class, 'bannerable')
            ->using(Bannerable::class)
            ->withPivot('id');
    }

    /**
     * Get all of the readlists for the banner.
     */
    public function readlists()
    {
        return $this->morphedByMany(Readlist::class, 'bannerable')
            ->using(Bannerable::class)
            ->withPivot('id');
    }
}

and in "BannerSchema" i have:

public function fields(): array
    {
        return [
            ID::make(),
            Str::make('name')->sortable(),
            MorphToMany::make('media', [
                BelongsToMany::make('series'),
                BelongsToMany::make('readlists'),
            ]),
        ];
    }

But in the response of the endpoint not show anything respect to the relationship:

{
    "jsonapi": {
        "version": "1.0"
    },
    "data": [
        {
            "type": "banners",
            "id": "1",
            "attributes": {
                "name": "Banner 1"
            },
            "relationships": {
                "media": {
                    "links": {
                        "related": "http://localhost/api/v2/banners/1/media",
                        "self": "http://localhost/api/v2/banners/1/relationships/media"
                    }
                }
            },
            "links": {
                "self": "http://localhost/api/v2/banners/1"
            }
        },
    ]
}

I don't understand what I'm doing wrong, could someone guide me?

@lindyhopchris
Copy link
Contributor

You need to use an include path to request the inclusion of related resources, e.g. /api/v1/banners?include=media

Here it is in the spec:
https://jsonapi.org/format/#fetching-includes

@maartenpaauw
Copy link

maartenpaauw commented May 15, 2024

@lindyhopchris I encountered a similar issue. I've created a separated issue #280.

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

No branches or pull requests

3 participants