Files
ladill-servers/app/Http/Controllers/Hosting/SearchController.php
T
isaaccladandCursor fc58c9d40d
Deploy Ladill Servers / deploy (push) Successful in 29s
Add a mobile search page for Servers.
Return an HTML search screen from /search for browser visits so the bottom nav Search tab opens a usable lookup UI instead of raw JSON.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-07 15:38:38 +00:00

60 lines
2.1 KiB
PHP

<?php
namespace App\Http\Controllers\Hosting;
use App\Http\Controllers\Controller;
use App\Models\CustomerHostingOrder;
use App\Models\HostingProduct;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
class SearchController extends Controller
{
public function __invoke(Request $request): JsonResponse|View
{
$q = trim((string) $request->query('q', ''));
$results = mb_strlen($q) >= 2 ? $this->resultsForUser($request->user(), $q) : [];
if ($request->expectsJson() || $request->ajax() || $request->wantsJson()) {
return response()->json(['results' => $results]);
}
return view('search', [
'query' => $q,
'results' => $results,
]);
}
/** @return list<array{type:string,title:string,subtitle:string,url:string}> */
private function resultsForUser($user, string $q): array
{
$like = '%'.$q.'%';
$results = [];
$serverTypes = [HostingProduct::TYPE_VPS, HostingProduct::TYPE_DEDICATED];
CustomerHostingOrder::query()
->forUser($user->id)
->whereHas('product', fn ($query) => $query->whereIn('type', $serverTypes))
->where(function ($query) use ($like) {
$query->where('domain_name', 'like', $like)
->orWhere('server_ip', 'like', $like);
})
->with('product')
->orderByDesc('updated_at')
->limit(12)
->get()
->each(function (CustomerHostingOrder $order) use (&$results): void {
$panelUrl = $order->control_panel_url ?: route('servers.orders.show', $order);
$results[] = [
'type' => 'server',
'title' => $order->domain_name ?: ($order->product?->name ?? 'Server #'.$order->id),
'subtitle' => ($order->product?->name ?? 'Server').' · '.ucwords(str_replace('_', ' ', (string) $order->status)),
'url' => $panelUrl,
];
});
return $results;
}
}