Closed genyded closed 1 year ago
By default you need to explicitly require the profile relationship in order for it to be included.
http://localhost/api/users/1?include=profile
Actually, that is for inclusion of the related data, not the links. Anyway, we figured it out. We had to add the relationships in the JSONAPI Resource files, but it seems the tutorial skips over that part. Closing since resolved.
<?php
namespace App\JsonApi\V1\Users;
use Illuminate\Http\Request;
use LaravelJsonApi\Core\Resources\JsonApiResource;
class UserResource extends JsonApiResource
{
/**
* Get the resource's attributes.
*
* @param Request|null $request
* @return iterable
*/
public function attributes($request): iterable
{
return [
'firstName' => $this->first_name,
'lastName' => $this->last_name,
'email' => $this->email,
$this->mergeWhen(auth('api')->user() && auth('api')->user()->isAdmin(), [
'admin' => $this->admin,
'createdAt' => $this->created_at,
'updatedAt' => $this->updated_at
]),
//'verificationToken' => $this->when($this->isAdmin() || (auth('api')->user() && auth('api')->user()->isAdmin()), $this->admin),
//'createdAt' => $this->when(auth('api')->user() && auth('api')->user()->isAdmin(), $this->created_at),
//'updatedAt' => $this->when(auth('api')->user() && auth('api')->user()->isAdmin(), $this->updated_at)
];
}
/**
* Get the resource's relationships.
*
* @param Request|null $request
* @return iterable
*/
public function relationships($request): iterable
{
return [
$this->relation('profile'),
];
}
}
OK, we're probably missing something simple here, but cannot seem to figure out what. We have two models User & Profile with a one-to-one relationship. The relationships work fine outside of JSONAPI (just standard Laravel). Our schemas are set up for both as follows:
When we query a user though, there is no reference at all to the profile:
There are no errors, and from the JSONAPI tutorial, we think we have all the pieces in place for this to work. So, what are we missing here?