42 lines
1.1 KiB
PHP
42 lines
1.1 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Http\Controllers;
|
||
|
|
|
||
|
|
use App\Models\Investigation;
|
||
|
|
use App\Models\Target;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use Illuminate\View\View;
|
||
|
|
|
||
|
|
class SearchController extends Controller
|
||
|
|
{
|
||
|
|
public function index(Request $request): View
|
||
|
|
{
|
||
|
|
$query = trim((string) $request->query('q', ''));
|
||
|
|
|
||
|
|
$investigations = collect();
|
||
|
|
$targets = collect();
|
||
|
|
|
||
|
|
if ($query !== '') {
|
||
|
|
$investigations = Investigation::query()
|
||
|
|
->accessibleBy($request->user())
|
||
|
|
->where('name', 'like', "%{$query}%")
|
||
|
|
->orderBy('name')
|
||
|
|
->get();
|
||
|
|
|
||
|
|
$targets = Target::query()
|
||
|
|
->whereHas('investigation', fn ($q) => $q->accessibleBy($request->user()))
|
||
|
|
->where('value', 'like', "%{$query}%")
|
||
|
|
->with('investigation')
|
||
|
|
->orderBy('value')
|
||
|
|
->limit(100)
|
||
|
|
->get();
|
||
|
|
}
|
||
|
|
|
||
|
|
return view('search.index', [
|
||
|
|
'query' => $query,
|
||
|
|
'investigations' => $investigations,
|
||
|
|
'targets' => $targets,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
}
|