🎉 سال نو، مهارت نو، مشاوره رایگان نقشه راه برنامه نویسی (آفر ویژه ثبت نام قبل از افزایش قیمت 🔥)
۰ ثانیه
۰ دقیقه
۰ ساعت
۰ علی منظور
پیاده سازی فیلتر محصولات با Scope Filters
جامعه پی اچ پی ایجاد شده در ۲۶ فروردین ۱۴۰۴

OrderbyFilter.php

<?php
namespace App\Http\Controllers\Filters;
use App\Models\Product;
use Illuminate\Database\Eloquent\Builder;
class OrderbyFilter
{
    public function newest(Builder $query)
    {
        return $query->orderBy('created_at', 'desc');
    }
    public function mostPopular(Builder $query)
    {
        return $query->orderBy('views', 'desc');
    }
    public function default(Builder $query)
    {
        return $query->latest(); // Default sorting
    }
    public function lowToHigh(Builder $query)
    {
        return $query->orderBy('price', 'asc');
    }
    public function highToLow(Builder $query)
    {
        return $query->orderBy('price', 'desc');
    }
}

ProductsController.php

public function index(Request $request)
    {
        $searchQuery = $request->input('search'); // Get the search query
        $products = Product::query()
            ->when($request->has('filter') && $request->filter === 'orderby', function ($query) use ($request) {
                $orderByFilter = new OrderbyFilter();
                $action = $request->action ?? 'default';
                if (method_exists($orderByFilter, $action)) {
                    $orderByFilter->{$action}($query);
                }
            })
            ->filters($request->except(['filter', 'action'])) // Other filters
            ->paginate(20)
            ->withQueryString();
        $categoryIds = $products->pluck('category_id')->unique();
        $categories = Category::whereIn('id', $categoryIds)
            ->withCount('products')
            ->orderByDesc('products_count')
            ->latest()
            ->take(10)
            ->get();
        return view('frontend.products.all', compact('products', 'categories', 'searchQuery'));
    }

frontend.products.all view

<div class="filter-col3 p-r-15 p-b-27 mr-auto">
                            <div class="mtext-102 cl2 p-b-15">
                                مرتب سازی براساس
                            </div>
                            <ul>
                                @php
                                    $filterOptions = [
                                        'default' => [
                                            'label' => 'پیش فرض',
                                            'action' => 'default',
                                        ],
                                        'mostPopular' => [
                                            'label' => 'محبوبیت',
                                            'action' => 'mostPopular',
                                        ],
                                        'newest' => [
                                            'label' => 'جدیدترین',
                                            'action' => 'newest',
                                        ],
                                        'lowToHigh' => [
                                            'label' => 'قیمت: کم به زیاد',
                                            'action' => 'lowToHigh',
                                        ],
                                        'highToLow' => [
                                            'label' => 'قیمت: زیاد به کم',
                                            'action' => 'highToLow',
                                        ],
                                    ];
                                    $currentAction = request('action', 'default');
                                @endphp
                                @foreach ($filterOptions as $key => $option)
                                    <li class="p-b-6">
                                        <a href="?filter=orderby&action={{ $option['action'] }}"
                                            class="filter-link stext-106 trans-04 {{ $currentAction === $option['action'] ? 'filter-link-active' : '' }}">
                                            {{ $option['label'] }}
                                        </a>
                                    </li>
                                @endforeach
                            </ul>
                        </div>