我像上面的代码一样根据两列的价格过滤器获得产品。我可以使用其他短代码或缩短此代码和任何 laravel 内置函数吗

I get product based on price filter on two column like above code. can I do with other short code or short this code and any laravel inbuilt function

我像上面的代码一样根据两列的价格过滤器获得产品。

我可以使用其他短代码或缩短此代码吗

public function index(Request $request)
{
    $products = Product::query()
        ->where('status', '1')
        ->when($request->range, fn ($query) => $query->whereBetween('actual_price', [0, 1600]))
        ->when($request->range, fn ($query) => $query->orWhereBetween('saleable_price', [0, 1600]))
        ->orderBy('saleable_price', 'asc')->get();
    return view('frontend.product.index', compact('products'));
}

您可以简单地将两个范围查询放入一个回调中

public function index(Request $request)
{
    $products = Product::query()
        ->where('status', '1')
        ->when($request->range, function ($query) { 
           $query->whereBetween('actual_price', [0, 1600])
              ->orWhereBetween('saleable_price', [0, 1600]);
        })
        ->orderBy('saleable_price', 'asc')->get();
    return view('frontend.product.index', compact('products'));
}