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

Unable to use MorphTo relationship #222

Open
niccolovettorello1997 opened this issue Jan 20, 2023 · 6 comments
Open

Unable to use MorphTo relationship #222

niccolovettorello1997 opened this issue Jan 20, 2023 · 6 comments

Comments

@niccolovettorello1997
Copy link

I'm trying create JSON APIs for a model which has a polymorphic morphTo relationship. I created the relationship following the documentation (in particular the section on morphTo present in this page), but I'm unable to use it correctly. I'll give you further details below.

My (simplified) structure is the following:

users
id
name

businesses
id
name

addresses
addressable_type
addressable_id
street_number

A user and a business can have multiple addresses, while a single address is associated either to a user or a business. This is summarized by a morphTo polymorphic relationship called addressable and leads to:

Addressable.php

class Address extends Model
{
  // Other code

  /**
     * An address morphs to an addressable
     * 
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
     */
    public function addressable()
    {
        return $this->morphTo('addressable');
    }
}

Business.php

class Business extends Model
{
  // Other code

  /**
     * A business may morph to many addresses
     * 
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
     */
    public function addresses()
    {
        return $this->morphMany(Address::class, 'addressable');
    }
}

Same for user

At this point I map the relationship onto the resources' schemas:

BusinessSchema.php

class BusinessSchema extends Schema
{
  // ....

  HasMany::make('addresses'),

  // ....
}

Same for user

AddressSchema.php

class AddressSchema extends Schema
{
  // ....

  MorphTo::make('addressable')->types('users', 'businesses'),

  // ....
}

Every other detail needed to map this relationship (for example in AddressRequest.php, or while defining the routes for addresses, businesses and users) is then filled in correctly. This is reasonably proved by the fact that every other relationship but this (polymorphic) one works fine.

Testing the newly implemented API I can create an address, I can modify and then delete it: everything seems to work perfectly.

Nonetheless, while trying to get the related business or user for an address, i.e. GET 127.0.0.1:8000/v1/addresses/1/addressable, I get the following error:

No schema for JSON:API resource type addressables

I can't figure out what the problem might be.

@atapatel
Copy link

@niccolovettorello1997 Have you registered your schema in Server.php ?

@lindyhopchris
Copy link
Contributor

Thanks for raising this issue. Reading it, I feel like I've missed something from the documentation, but it has been a while since I used a morph-to relationship.

Can you provided a stack trace for where the No schema for JSON:API resource type addressables error is originating from? It'll help me work out what's going on and potentially whether I've missed something from the documentation.

@niccolovettorello1997
Copy link
Author

@atapatel yep, everything is correctly registered.

@lindyhopchris sure, here it is:

Screenshot_2023-01-25_15-14-22

I excluded the first 40 entries that are related to the framework itself, copying only the part that was raised by the package laravel json api.

@niccolovettorello1997
Copy link
Author

Hi @lindyhopchris, do you have any news regarding this problem?

Thanks a lot

@lindyhopchris
Copy link
Contributor

Sorry, just not had the time.

I believe you need to follow what is explained here when discussing polymorphic to-many relationships, i.e. it's the same issue with polymorphic to-one relations:
https://laraveljsonapi.io/docs/2.0/digging-deeper/polymorphic-to-many.html#query-parameters

@codocedo
Copy link

codocedo commented May 5, 2023

Hello!
I tried to follow the polymorphic-to-many chapter and adapt it to polymorphic-to-one relations, however I keep on having the same issues reported by OP.

Particularly, I have a morphTo relation between model A and models B and C. I will call this relation X.

What I did:

  • I wrote both XQuery and XCollectionQuery.

  • In ASchema, I included in method fields():
    MorphTo::make('X')->types('B','C')

  • In api.php I included:
    $server->resource(A, JsonApiController::class)->relationships(function ($relations) {$relations->hasOne('X');)};

  • In Server.php, I included in method serving():
    LaravelJsonApi::registerCollectionQuery(XQuery::class, 'X');
    LaravelJsonApi::registerCollectionQuery(XCollectionQuery::class, 'Xs');

  • Xs stands for the plural of X.

Yet, when I try a GET over api/v1/As/1/X, I still get the answer

LogicException: No schema for JSON:API resource type X.

I do get proper results for a GET over api/v1/As?include=X.

Any suggestion will be appreciated.
Thanks!

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

4 participants