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);
    }
star

Sat Jan 20 2024 02:11:01 GMT+0000 (Coordinated Universal Time) https://github.com/LaravelDaily/laravel-tips/blob/master/db-models-and-eloquent.md

#laravel #database #eloquent #model
star

Sat Jan 20 2024 02:08:49 GMT+0000 (Coordinated Universal Time) https://github.com/LaravelDaily/laravel-tips/blob/master/collections.md

#laravel #eloquent #collection
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

Save snippets that work with our extensions

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