laravel icon indicating copy to clipboard operation
laravel copied to clipboard

[Question] Schema required for polymorphic relationship

Open DenisaHalmaghi opened this issue 4 years ago • 6 comments

Hi @lindyhopchris, love the package you created.

I am confused about an aspect about Polymorphic To-Many relationships because of the following problem: I have the following schema:

class GeolevelableSchema extends Schema
{
  public function fields(): array
  {
      return [
          ID::make(),
          MorphToMany::make('geolevels', [
                  BelongsToMany::make('regions')->type('regions'),
                  BelongsToMany::make('countries')->type('countries'),
           ]);
          DateTime::make('createdAt')->sortable()->readOnly(),
          DateTime::make('updatedAt')->sortable()->readOnly(),
      ];
  }
}

I have created a GeolevelCollectionQuery class as stated in the documentation, but when i access an endpoint like this: DELETE /geolevelable/id/relationships/geolevels it throws this error :

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

Am I supposed to create a special kind of schema for the geolevels "resource" (there are schemas for regions and countries already)? That would be like creating a schema for the "media" type (the Polymorphic To-Many example from the documentation ) - something which makes little sense to me.

Am I missing something?

Thank you for your time!

DenisaHalmaghi avatar Aug 11 '21 13:08 DenisaHalmaghi

If there is a schema for the geolevels model, you should register it in your Server.php.

ben221199 avatar Aug 11 '21 15:08 ben221199

Well sure, but there is no geolevels model because it's just a name to group countries and regions under a single relationship, just like media in the documentation.

DenisaHalmaghi avatar Aug 12 '21 06:08 DenisaHalmaghi

I'm on holiday at the moment so can't look into this in detail. What's the stack trace of that exception? Wondering where it is being thrown from.

lindyhopchris avatar Aug 12 '21 10:08 lindyhopchris

Hi, sorry for the delay, I've also been away on holiday. Here is a stack trace for the problem. Unfortunately, PHPUnit did not provide the stack trace for this so i uploaded a photo taken while using the debugger.

image

DenisaHalmaghi avatar Aug 30 '21 14:08 DenisaHalmaghi

Hello @lindyhopchris, have you got any time to take a look at this?

DenisaHalmaghi avatar Sep 16 '21 21:09 DenisaHalmaghi

I haven't unfortunately - extremely limited on open source time at the moment.

Just looking at your original post, you've called the class GeolevelableSchema. But it's probably looking for GeolevelSchema. Does that fix it?

lindyhopchris avatar Sep 17 '21 08:09 lindyhopchris