Unable to use MorphTo relationship
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.
@niccolovettorello1997 Have you registered your schema in Server.php ?
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.
@atapatel yep, everything is correctly registered.
@lindyhopchris sure, here it is:

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.
Hi @lindyhopchris, do you have any news regarding this problem?
Thanks a lot
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
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
XQueryandXCollectionQuery. -
In
ASchema, I included in methodfields():
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'); -
Xsstands for the plural ofX.
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!