Deploy Ladill Servers / deploy (push) Successful in 29s
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>
60 lines
2.1 KiB
PHP
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;
|
|
}
|
|
}
|