validate([ 'q' => ['nullable', 'string', 'max:120'], 'sector' => ['nullable', 'string', 'max:80'], 'province' => ['nullable', 'string', 'max:80'], 'maturity_level' => ['nullable', 'integer', 'between:1,6'], 'per_page' => ['nullable', 'integer', 'min:1', 'max:50'], 'min_amount' => ['nullable', 'numeric', 'min:0', 'max:999999999999'], 'max_amount' => ['nullable', 'numeric', 'min:0', 'max:999999999999', ...($request->filled('min_amount') ? ['gte:min_amount'] : [])], 'currency' => ['nullable', 'required_with:min_amount,max_amount', 'in:MZN,USD,EUR,ZAR'], ]); $query = Opportunity::with('sector:id,name,slug')->published(); foreach (['min_amount' => '>=', 'max_amount' => '<='] as $field => $operator) { if (isset($validated[$field])) { $query->where('estimated_investment_amount', $operator, $validated[$field]); } } if (! empty($validated['currency'])) { $query->where('estimated_investment_currency', $validated['currency']); } if (! empty($validated['q'])) { $term = $validated['q']; $query->where(fn ($q) => $q ->whereLike('title', "%{$term}%") ->orWhereLike('description', "%{$term}%") ->orWhereLike('summary', "%{$term}%") ->orWhereLike('reference', "%{$term}%") ->orWhereLike('province', "%{$term}%") ->orWhereHas('sector', fn ($sectorQuery) => $sectorQuery ->whereLike('name', "%{$term}%") ->orWhereLike('slug', "%{$term}%"))); } if (! empty($validated['sector'])) { $sector = $validated['sector']; \App\Services\SectorOrganization::constrain($query, $sector); } if (! empty($validated['province'])) { $query->where('province', $validated['province']); } if (! empty($validated['maturity_level'])) { \App\Services\OpportunityMaturity::constrainSearch($query, 'historical:'.$validated['maturity_level']); } $results = $query->orderByDesc('is_featured') ->orderByDesc('published_at') ->paginate($validated['per_page'] ?? 12); return response()->json($results); } public function show(string $slug): JsonResponse { $opportunity = Opportunity::with('sector:id,name,slug') ->where('slug', $slug)->published()->firstOrFail(); return response()->json(['success' => true, 'data' => $opportunity]); } }