@vincent15000 What do you Infrastructure and Port models look like?
Jul 3, 2024
19
Level 63
API resources with relationships
Hello,
I need help to understand why I have relationships added in my API ressource although I don't have loaded them.
public function infrastructures()
{
Gate::authorize('viewAny', Infrastructure::class);
$infrastructures = Infrastructure::with('ports')->orderBy('name')->get();
return new InfrastructureCollection($infrastructures);
}
...
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'name' => $this->name,
'trash_port' => $this->trash_port,
'ports' => new PortCollection($this->whenLoaded('ports')),
'branches' => new BranchCollection($this->whenLoaded('branches')),
'can_update' => auth()->user()?->can('update', $this->resource) ?? false,
];
}
I get a response with the ports but also with all ports' nested relationships.
But I only need the port API resource without any relationship.
If I had eager loaded ('ports.endoint'), I would have understood, but the endpoints are not loaded.
Can you help me understand why I get the endpoints are loaded too ?
Thanks for your help.
V
Please or to participate in this conversation.