Deploy Ladill QR Plus / deploy (push) Successful in 40s
Introduce a dedicated /search page, wire the bottom nav and topbar to it, and expose JSON results for live lookup of QR codes. Co-authored-by: Cursor <cursoragent@cursor.com>
52 lines
1.6 KiB
PHP
52 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\QrCode;
|
|
use App\Support\Qr\QrTypeCatalog;
|
|
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->results($q) : [];
|
|
|
|
if ($request->expectsJson() || $request->ajax() || $request->wantsJson()) {
|
|
return response()->json(['results' => $results]);
|
|
}
|
|
|
|
return view('search.index', [
|
|
'query' => $q,
|
|
'results' => $results,
|
|
]);
|
|
}
|
|
|
|
/** @return list<array{type: string, title: string, subtitle: string, url: string}> */
|
|
private function results(string $q): array
|
|
{
|
|
$like = '%'.$q.'%';
|
|
|
|
return ladill_account()->qrCodes()
|
|
->whereIn('type', QrTypeCatalog::plusTypes())
|
|
->where(function ($query) use ($like): void {
|
|
$query->where('label', 'like', $like)
|
|
->orWhere('short_code', 'like', $like)
|
|
->orWhere('destination_url', 'like', $like);
|
|
})
|
|
->orderByDesc('updated_at')
|
|
->limit(15)
|
|
->get()
|
|
->map(fn (QrCode $code): array => [
|
|
'type' => 'qr',
|
|
'title' => $code->label,
|
|
'subtitle' => $code->typeLabel().' · '.$code->publicUrl(),
|
|
'url' => route('user.qr-codes.show', $code),
|
|
])
|
|
->all();
|
|
}
|
|
}
|