-
Notifications
You must be signed in to change notification settings - Fork 107
Schemas loading for relationships #8
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
Comments
I had a problem like this, I can't remember if this was what fixed it but it might be not detecting the Addons model properly if empty. I created a parent "Schema" class to add in a few extra tools like this: <?php
namespace App\Schemas;
use Neomerx\JsonApi\Schema\SchemaProvider;
use Illuminate\Database\Eloquent\Collection;
abstract
class Schema extends SchemaProvider
{
public
function getId($resource)
{
return $resource->id;
}
protected
function extractRelation($resource, $relationName, $force = false)
{
if (empty($resource->{$relationName}) && $force)
{
$resource->load($relationName);
}
if (empty($resource->{$relationName}))
{
return null;
}
if ($resource->{$relationName} instanceof Collection)
{
return $resource->{$relationName}->all();
}
return $resource->{$relationName};
}
} And then when I extract the relationship I do it like: <?php
namespace App\Schemas;
use App\Schemas\Schema;
class `ShipmentSchema` extends Schema
{
...
public
function getRelationships($shipment, array $includeRelationships = [])
{
return [
'addons' => [
self::DATA => $this->extractRelation($shipment, 'addons')
],
];
}
} |
@phaberest Sorry for slow reply - I've been on holiday the last two weeks. The error you're getting is a Does that fix it? |
Thanks to both of you, I have temporarily put it in the parent element Tried on fly @lindyhopchris solution and it seems to look for the schema 👍 I'll look into it, thanks a lot! |
Hi @lindyhopchris and thanks in advance for this awesome work!
I'm getting an error when I try to add a relationship in my schema, it seems it can't find the schema for the relationship, even if I've set it in the configuration file.
The error I get is
In
json-api.php
I set the schemas array as followsAnd in the parent model schema I have
I'm having troubles understanding the docs 100%, maybe I did something wrong. Maybe I'm not including enough from neomerx/json-api...I am jumping from an error to another 😔
Thanks for your help
The text was updated successfully, but these errors were encountered: