4
在Laravel 5.5 documentation,下条件关系,它说Laravel 5.5雄辩WhenLoaded关系
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
(型号)?
据我了解,如果您已加载通过'型号枯萎的关系::用(“角色”)的'roles'将充满'或'$ modelObject->加载( “角色”)' – apokryfos