$files * @return array file id => URL, for the files that have one */ public function forMany(Collection $files, User $client): array { $own = $files ->filter(fn (File $file): bool => $file->uploaded_by === $client->id) ->pluck('id') ->map(fn ($id): int => (int) $id) ->all(); if ($own === []) { return []; } $links = ShareLink::query() ->where('shareable_type', (new File)->getMorphClass()) ->whereIn('shareable_id', $own) ->where('created_by', $client->id) // Oldest first, so a file that somehow carries two is // described by the one the client has already been given // rather than by whichever the database returned today. ->orderBy('id') ->get(); $urls = []; foreach ($links as $link) { $fileId = (int) $link->shareable_id; if (isset($urls[$fileId]) || ! $link->isActive()) { continue; } $urls[$fileId] = route('share.show', $link->token); } return $urls; } }