sarala
sarala copied to clipboard
API Documentation missing
Hi Milroy,
You explain very well how to properly configure the javascript models, however there's nothing that can help me configuring a proper format for the response from my API. Both, a single resource and a collection are working fine, I'm struggling with the relationships though. I'm using the Laravel's Resources, and I tried to reverse engineer the json-formatter to find out the right format, but I could not.
Here is an exampe of my response(s):
public function toArray($request): array
{
return [
'id' => $this->id,
'type' => 'whatever_type',
'attributes' => [
'my' => 'data', // that's working fine, it's my data
// Attempt 1: this next line doesn't fill my JS model
'rel_1' => new MyRelResource($this->rel_1)->toArray($request)
// Attempt 2: nothing either:
'rel_2' => ['data' => new MyRelResource($this->rel_2)->toArray($request)
],
'relationships' => [
// Attampt 3: The line below doesn't fill my relationship in the JS model either
'rel_3' => new MyRelResource($this->rel_3)->toArray($request)
// Attempt 4: still not getting the JS model filled:
'rel_4' => ['data' => new MyRelResource($this->rel_4)->toArray($request)
]
];
}
I also tried adding the rel_ properties at the same level than the attributes property with no result.
What is the exact format I need to follow in order to get my relationships filled in the front end? I followed a link from other issue where you reference the good practices in a API, but couldn't achieve anything either.
Thanks, and thanks for the package
Hi @Garanaw,
According to JSON:API spec in api response data.relationships should only contain reference objects. And data belongs to relationships should go inside included
https://laravel-news.com/json-api-introduction
Best,
@milroyfraser That's great, finally I got the right format. Thanks! Might I suggest to add this page as well as other links I've seen in other issues to a wiki page as a reference? That'd save us time trying to find some documentation, and some of the time you spend answering this kind of questions too. Again, thank you.
P.S. Issue ready to be closed if you agree
@milroyfraser Again struggling with relationships, collections this time. I followed the exact format shown in the link above (literally copy/paste the example), yet the formatter complains about "data is undefined". It works fine with a single item in the relationships/included fields ( 'included' => ['includedItem' => new Resource($item)] ).
Any specific format for this use case?
Thank you
Hi @Garanaw, I cannot speak for the author, but I think this package assumes a familiarity with the JSON:API specification, so that kind of documentation doesn't really belong here.
That said, so you aren't repeating work that's already been done, I highly recommend you check out CloudCreativity's Laravel-JSON-API package for Laravel. It is an extensive package that implements the best practices descriped by that JSON:API spec.