*/ public function toArray(Request $request): array { $user_id = Auth::id(); return [ 'id' => $this->id, 'name' => $this->name, 'is_public' => $this->is_public, 'owner' => $this->listOwner->username, 'role' => $this->getRole($this->owner, $user_id), 'collaborators' => $this->whenLoaded('collaborators', fn () => $this->collaborators->map(fn (User $user) => [ 'id' => $user->getKey(), 'username' => $user->username, 'role' => $user->pivot->getAttribute('role_id'), ])), 'movies' => $this->whenLoaded('movies'), ]; } private function getRole(int $owner_id, int $user_id): ?string { if ($owner_id === $user_id) { return 'OWNER'; } return $this->getUserRole($user_id); } }