public function index(Travel $travel, ToursListRequest $request) { $tours = $travel->tours() ->when($request->priceFrom, function ($query) use ($request) { $query->where('price', '>=', $request->priceFrom * 100); }) ->when($request->priceTo, function ($query) use ($request) { $query->where('price', '<=', $request->priceTo * 100); }) ->when($request->dateFrom, function ($query) use ($request) { $query->where('starting_date', '>=', $request->dateFrom); }) ->when($request->dateTo, function ($query) use ($request) { $query->where('starting_date', '<=', $request->dateTo); }) ->when($request->sortBy, function ($query) use ($request) { if (! in_array($request->sortBy, ['price']) || (! in_array($request->sortOrder, ['asc', 'desc']))) { return; } $query->orderBy($request->sortBy, $request->sortOrder); }) ->orderBy('starting_date') ->paginate(); return TourResource::collection($tours); }