db); $key = $this->resolveEntityString(); $resource = $this->entity::query(); $template = Design::withTrashed()->find($this->decodePrimaryKey($this->template)); $template_service = new TemplateService($template); if($this->entity == Invoice::class) { $resource->with('payments', 'client'); } $result = $resource->withTrashed() ->whereIn('id', $this->transformKeys($this->ids)) ->where('company_id', $this->company->id) ->get(); if($result->count() <= 1) $data[$key] = collect($result); else $data[$key] = $result; $pdf = $template_service->build($data)->getPdf(); if($this->send_email) $this->sendEmail($pdf); else { $filename = "templates/{$this->hash}.pdf"; Storage::disk(config('filesystems.default'))->put($filename, $pdf); } } private function sendEmail(mixed $pdf): mixed { //send the email. return $pdf; } /** * Context * * If I have an array of invoices, what could I possib * * */ private function resolveEntityString() { return match ($this->entity) { Invoice::class => 'invoices', Quote::class => 'quotes', Task::class => 'tasks', Credit::class => 'credits', RecurringInvoice::class => 'recurring_invoices', Project::class => 'projects', Expense::class => 'expenses', Payment::class => 'payments', Product::class => 'products', PurchaseOrder::class => 'purchase_orders', Project::class => 'projects', Client::class => 'clients', Vendor::class => 'vendors', }; } public function middleware() { return [new WithoutOverlapping("template-{$this->company->company_key}")]; } }