feat: search params
This commit is contained in:
parent
b7439d6f63
commit
83f92dd7a1
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Enums\HuntDifficulty;
|
||||
use App\Http\Requests\StoreHuntRequest;
|
||||
use App\Http\Requests\UpdateHuntRequest;
|
||||
use App\Models\Hunts;
|
||||
|
|
@ -105,6 +106,54 @@ class HuntsController extends Controller
|
|||
}
|
||||
}
|
||||
|
||||
if ($request->filled('city')) {
|
||||
$city = $request->query('city');
|
||||
|
||||
if (is_string($city)) {
|
||||
$query->where('city', 'like', '%'.$city.'%');
|
||||
}
|
||||
}
|
||||
|
||||
if ($request->filled('difficulty')) {
|
||||
$difficulties = $request->query('difficulty');
|
||||
|
||||
if (is_string($difficulties)) {
|
||||
$difficulties = explode(',', $difficulties);
|
||||
}
|
||||
|
||||
if (is_array($difficulties)) {
|
||||
$validDifficulties = array_values(array_intersect(
|
||||
HuntDifficulty::values(),
|
||||
array_map('strtolower', $difficulties)
|
||||
));
|
||||
|
||||
if (! empty($validDifficulties)) {
|
||||
$query->whereIn('difficulty', $validDifficulties);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$startFrom = $this->parseDateFilter($request->query('start_from'));
|
||||
$startTo = $this->parseDateFilter($request->query('start_to'));
|
||||
$endFrom = $this->parseDateFilter($request->query('end_from'));
|
||||
$endTo = $this->parseDateFilter($request->query('end_to'));
|
||||
|
||||
if ($startFrom) {
|
||||
$query->where('start_at', '>=', $startFrom);
|
||||
}
|
||||
|
||||
if ($startTo) {
|
||||
$query->where('start_at', '<=', $startTo);
|
||||
}
|
||||
|
||||
if ($endFrom) {
|
||||
$query->where('end_at', '>=', $endFrom);
|
||||
}
|
||||
|
||||
if ($endTo) {
|
||||
$query->where('end_at', '<=', $endTo);
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
|
|
@ -121,6 +170,19 @@ class HuntsController extends Controller
|
|||
return $searchTerm !== '' ? $searchTerm : null;
|
||||
}
|
||||
|
||||
private function parseDateFilter(mixed $value): ?Carbon
|
||||
{
|
||||
if (! is_string($value) || trim($value) === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return Carbon::parse($value);
|
||||
} catch (\Throwable $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
|
|
|
|||
Loading…
Reference in New Issue