model->newQuery() ->with(['customer', 'orderItems.product', 'orderItems.service', 'invoice']) ->whereHas('orderItems.product', fn ($q) => $q->where('product_type', ProductTypeEnums::DOMAIN->value) ) ->orderByDesc('created_at'); if (!empty($filters['status'])) { $query->whereHas('orderItems.service', fn ($q) => $q->where('status', $filters['status']) ); } if (!empty($filters['customer'])) { $s = $filters['customer']; $query->whereHas('customer', fn ($q) => $q->where('fullname', 'like', "%{$s}%") ->orWhere('email', 'like', "%{$s}%") ); } if (!empty($filters['domain'])) { $d = $filters['domain']; $query->whereHas('orderItems', fn ($q) => $q->where('domain_name', 'like', "%{$d}%") ); } if (!empty($filters['from'])) { $query->where('created_at', '>=', $filters['from']); } if (!empty($filters['to'])) { $query->where('created_at', '<=', $filters['to'] . ' 23:59:59'); } return $query->paginate($perPage); } public function findDomainOrder(int $id): ?Order { return $this->model->newQuery() ->with(['customer', 'orderItems.product', 'orderItems.service', 'invoice.invoiceItems']) ->whereHas('orderItems.product', fn ($q) => $q->where('product_type', ProductTypeEnums::DOMAIN->value) ) ->find($id); } public function countPending(): int { return $this->model->newQuery() ->whereHas('orderItems.product', fn ($q) => $q->where('product_type', ProductTypeEnums::DOMAIN->value) ) ->whereHas('orderItems.service', fn ($q) => $q->where('status', 'pending') ) ->count(); } public function hasPendingOrActiveDomainByName(string $normalizedDomain): bool { return Service::whereHas('orderItem', fn ($q) => $q->where('domain_name', $normalizedDomain) ) ->whereNotIn('status', ['cancelled', 'refunded', 'terminated']) ->exists(); } }