On the Laravel 5.5 documentation, under Conditional Relationships, it says
whenLoaded method may be used to conditionally load a relationship
I tried in my code
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'email' => $this->email,
'roles' => Role::collection($this->whenLoaded('roles')),
'remember_token' => $this->remember_token,
];
}
According to the documentation, the roles key is removed from the resource response entirely before it is sent to the client because the relationship hasn't been loaded.
How do I load a relationship? How do I determine if a relationship is loaded? In this case how do I load Role (model)?