简体   繁体   English

Laravel 价格范围过滤器

[英]Laravel price range filter

I have to search price range(min_price & max_price) from two columns(regular_price & sale_price) but unable to get values from both columns.我必须从两列(regular_price 和 sale_price)中搜索价格范围(min_price 和 max_price),但无法从两列中获取值。

I currently have something like this:我目前有这样的事情:

My problem is ajax我的问题是 ajax

blade.php刀片.php

<div class="card mb-3">
    <div class="card-body">
        <p>
            <label for="amount">amount:</label>
            <input type="text" name="amount" id="amount" readonly class="border-0 fw-bold text-warning">
        </p>
        <div id="slider-range"></div>
    </div>
</div>

script.js脚本.js

<script src="{{ asset('themes/js/jquery-ui.js') }}"></script>
<script>
    $( function() {
        $( "#slider-range" ).slider({
            range: true,
            min: 0,
            max: 500,
            values: [ 75, 300 ],
            slide: function( event, ui ) {
                $( "#amount" ).val( "$" + ui.values[ 0 ] + " - $" + ui.values[ 1 ] );
            }
        });
        $( "#amount" ).val( "$" + $( "#slider-range" ).slider( "values", 0 ) +
            " - $" + $( "#slider-range" ).slider( "values", 1 ) );
        $.ajax({
            method: 'get',
            url: '{{ route('price') }}',
            data: formData,
            success: function (data) {

            }
        });
    });
</script>

web.php web.php

Route::any('/category/{categorySlug}', [App\Http\Controllers\CategoryController::class, 'price'])->name('price');

CategoryController.php类别Controller.php

public function price(Category $category, Request $request)
{
    $categories = Category::all();
    $colors = Color::all();
    $brands  = Brand::all();
    $min_price = Product::min('price');
    $max_price = Product::max('price');
    $filter_min_price = $request->min_price;
    $filter_max_price = $request->max_price;
    $range = [$filter_min_price, $filter_max_price];
    $products = Product::query()->whereBetween('price', $range)->get();

    if($filter_min_price && $filter_max_price){
        if($filter_min_price > 0 && $filter_max_price > 0)
        {
            $products = Product::all()->whereBetween('price', [$filter_min_price, $filter_max_price]);
        }
    } else {
        $products = Product::all();
    }
    return view('Home.contents.category',compact('products','categories','min_price','max_price','filter_min_price','filter_max_price', 'category', 'colors', 'brands'));
}

I removed everything that seems unnecessarry for the core of the question.我删除了对问题的核心来说似乎不必要的所有内容。 I extracted filter functionality to a seperate method, but in my project I would put it in a seperate class. I wrote the code here, so it might not work directly, but the logic should be correct.我将过滤器功能提取到一个单独的方法中,但在我的项目中我会把它放在一个单独的 class 中。我在这里写了代码,所以它可能不能直接工作,但逻辑应该是正确的。

public function price(Category $category, Request $request)
{
    $products = $this->filterByPrice(
        Product::query(),
        [$request->min_price ?: 0, $request->max_price ?: 0]
    );

    return ...;
}

private function filterByPrice($query, $range)
{
    if (!$range[0] && !$range[1]) return $query->all();
    
    if ($range[0] && !$range[1]) {
        return $query
            ->where('price', '>=', $range[0])
            ->orWhere('sale_price', '>=', $range[0])
            ->all();
    }

    if (!$range[0] && $range[1]) {
        return $query
            ->where('price', '<=', $range[1])
            ->orWhere('sale_price', '<=', $range[1])
            ->all();
    }

    return $query
        ->whereBetween('price', $range)
        ->orWhereBwtween('sale_price', $range)
        ->all();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM