4
는,이 방법은 조건부로 내 코드에서 시도Laravel 5.5 웅변 WhenLoaded 관계 <a href="https://laravel.com/docs/5.5/eloquent-resources" rel="nofollow noreferrer">Laravel 5.5 documentation</a>에
의 관계를로드 할 수 있습니다 whenLoaded
을 말한다
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, ]; }
설명서에 따르면 역할 키는 관계가로드되지 않았기 때문에 클라이언트에 전송되기 전에 리소스 응답을 모두 보냅니다.
관계를로드하려면 어떻게해야합니까? 관계가로드되었는지 어떻게 결정합니까? 이 경우
Role
(모델)을 어떻게로드합니까?
내가 이해하는 것처럼, 관계가'Model :: with ("roles")'또는'$ modelObject->를 통해 쇠약 해지면'roles'가 채워질 것입니다. load ("roles")' – apokryfos