db); $key = $this->resolveEntityString(); $entity = new $this->entity(); $template = Design::withTrashed()->find($this->decodePrimaryKey($this->template)); $template_service = new TemplateService($template); $resource = $entity->query() ->withTrashed() ->whereIn('id', $this->transformKeys($this->ids)) ->where('company_id', $this->company->id); if($this->entity == Invoice::class) $resource->with('payments','client'); $resource->get(); if(count($resource) <= 1) $data[$key] = [$resource]; else $data[$key] = $resource; $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}")]; } }