first commit
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\GetGigsRequest;
|
||||
use App\Http\Resources\GigResource;
|
||||
use App\Models\ORM\Gig;
|
||||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||
|
||||
class GigsController extends Controller
|
||||
{
|
||||
public function index(GetGigsRequest $request): AnonymousResourceCollection
|
||||
{
|
||||
// Используем eager loading (with), чтобы избежать проблемы N+1 запросов к категориям
|
||||
$query = Gig::with('categories');
|
||||
|
||||
// Фильтрация по нескольким категориям (Many-to-Many)
|
||||
if ($request->filled('categories')) {
|
||||
$query->whereHas('categories', function ($q) use ($request) {
|
||||
$q->whereIn('categories.id', $request->input('categories'));
|
||||
});
|
||||
}
|
||||
|
||||
// Фильтрация по промежутку дат
|
||||
if ($request->filled('date_from')) {
|
||||
$query->whereDate('event_date', '>=', $request->input('date_from'));
|
||||
}
|
||||
|
||||
if ($request->filled('date_to')) {
|
||||
$query->whereDate('event_date', '<=', $request->input('date_to'));
|
||||
}
|
||||
|
||||
// Сортируем события: сначала ближайшие
|
||||
$query->orderBy('event_date', 'asc');
|
||||
|
||||
// Пагинация по 10 элементов на страницу
|
||||
$gigs = $query->paginate(10);
|
||||
|
||||
return GigResource::collection($gigs);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user