The Laravel 5.5 documentation in the Conditional Relationships section says:
whenLoaded method can be used to conditionally load relationships
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 role key is completely removed from the resource response before it is sent to the client because the connection was not loaded.
How do I upload a relationship? How to determine if a link is loaded? In this case, how to load Role (model)?
source share