php Laravel价格范围过滤器

lb3vh1jj  于 2023-08-02  发布在  PHP
关注(0)|答案(3)|浏览(127)

我必须从两列(regular_price & sale_price)中搜索价格范围(min_price & max_price),但无法从这两列中获取值。
我现在有这样的东西:

**我的问题 AJAX **
blade.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

<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

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

CategoryController.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'));
}

kxxlusnw

kxxlusnw1#

我删除了所有似乎不必要的问题的核心。我将过滤器功能提取到一个单独的方法中,但在我的项目中,我会把它放在一个单独的类中。我在这里写了代码,所以它可能不能直接工作,但逻辑应该是正确的。

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

字符串

ubby3x7f

ubby3x7f2#

产品页面
$(function(){ $(“#slider-range”).slider({ range:true,min:0,max:500,slide:function(event,ui){ $('#amount').瓦尔(ui.values[0]+”-“+ui.values[1]); var price1 = ui.values[0]; var price2 = ui.values[1];[2019 - 04 - 29]【2019 - 04 - 29】【2019 - 04 - 29】【2019 - 04 - 04】【2019 - 04 - 04】【2019 - 04】【2019】【2019 - 04】【2019】【2019】【$. AJAX ({ type:“post”,url:url,data:{ p1:price1,p2:price2,},success:function(data){ $('#priceDiv').hide();$('#pricefilter').html(data);} }); } });});

llmtgqce

llmtgqce3#

//产品页面
$(function(){ $(“#slider-range”).slider({ range:true,min:0,max:5,slide:function(event,ui){ $(“#amount”).瓦尔(ui.values[0]+"-"+ui.values[1]); var price1 = ui.values[0]; var price2 = ui.values[1];[2019 - 04 - 21][2019 - 04 - 04][2019 - 04 - 04][2019 - 04]2019 - 04 - 04][2019 - 04][2019 - 0 $. AJAX ({ type:“post”,url:url,data:{ p1:price1,p2:price2,},success:function(data){ $('#div').hide();$('#price').html();},}); },});
});

相关问题