Open
Description
As its polymorphic nature, I can't know in advance what resource type it will be.
class AddressResource extends JsonApiResource
{
public $attributes = [
'label',
'geo_point',
];
public $relationships = [
'owner' => UserResource::class, // But also can be TeamResource, RestaurantResource, etc
];
}
Is there a way to check loaded $resource->owner->getTable()
to guest the corresponding type?
Or maybe a trait included in my model with the method getJsonApiResouce()
: $this->resource->owner->getJsonApiResource()
class User extends Model IHasJsonApiResource
{
use HasFactory;
/**
* @return class-string<JsonApiResource>
*/
public function getJsonApiResouce(): string
{
return UserResource::class;
}
}
Or a similar way of doing it, as User::factory()
works.
Metadata
Metadata
Assignees
Labels
No labels