Snippets Collections
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);
    }
const prices = [400.50, 3000, 99.99, 35.99, 12.00, 9500];


// Sorting books by their rating: // use slice to copy the array
const acscendingsOrders = prices.slice().sort((a, b) => a.toFixed(2) - b.toFixed(2))
const descendingOrders = prices.slice().sort((a, b) => b.toFixed(2) - a.toFixed(2))

/** The indexOf() method
	Returns the position of the first occurrence of a value in a string.
	The indexOf() method returns -1 if the value is not found.
	The indexOf() method is case sensitive.
  */


function isValidPassword(password, username) {
	if (
		password.length < 8 ||
		password.indexOf(' ') !== -1 ||
		password.indexOf(username) !== -1
	) {
		return false;
	}
	return true;
}

/** or */


function isValidPassword(password, username) {
	const tooShort = password.length < 8;
	const hasSpace = password.indexOf(' ') !== -1;
	const tooSimilar = password.indexOf(username) !== -1;
	if (tooShort || hasSpace || tooSimilar) return false;
	return true;
}
function betweenYears(yearStart, yearEnd){
    return function(number){
          if(number >= yearStart && number <= yearEnd){
              return number
          }
          return false
    }
}

const myYear = betweenYears(1900, 1999);
myYear(1978);
star

Sat Jan 20 2024 01:44:36 GMT+0000 (Coordinated Universal Time) https://github.com/LaravelDaily/Laravel-Travel-API-Course/blob/main/app/Http/Controllers/Api/V1/TourController.php

#laravel #php #filtering #search #eloquent
star

Wed Apr 12 2023 05:58:05 GMT+0000 (Coordinated Universal Time)

#function #return #values #filtering
star

Wed Apr 12 2023 01:00:20 GMT+0000 (Coordinated Universal Time)

#function #return #values #filtering
star

Wed Apr 12 2023 00:20:59 GMT+0000 (Coordinated Universal Time)

#function #return #values #filtering

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension