customer(); return view('livewire.invoices', [ 'invoices' => $this->invoices($customer?->id), ]); } /** * This customer's invoices, and nobody else's. * * Scoped by `customer_id` in the query rather than filtered afterwards, and * an empty page for somebody with no customer record at all — an operator * signed into the portal, an account whose customer has been deleted. The * document itself is scoped again at the download route: a list is a list, * and hiding a link has never stopped anybody fetching a URL. * * @return LengthAwarePaginator */ private function invoices(?int $customerId): LengthAwarePaginator { if ($customerId === null) { return new Paginator([], 0, 12); } return Invoice::query() ->where('customer_id', $customerId) ->orderByDesc('issued_on') // Then by id, because a day can hold several documents and a list // whose order changes between two page loads reads as a fault. ->orderByDesc('id') ->paginate(12); } }