在 Laravel 中搜索两个数字(价格)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31975250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:10:13  来源:igfitidea点击:

Searching between two numbers(prices) in Laravel

phpmysqllaravellaravel-4eloquent

提问by Brandon

I'm trying to run an eloquent query to filter results by category, then title, then price. category and title work as they should but when i added BETWEEN for searching between two prices it broke. Does anyone know how i could fix my query?

我正在尝试运行一个雄辩的查询,以按类别、标题和价格过滤结果。类别和标题正常工作,但是当我添加 BETWEEN 以在两个价格之间进行搜索时,它打破了。有谁知道我如何解决我的查询?

@foreach(Inventory::where('category', '=', $cat)->where('title', 'LIKE', '%'. $search_query .'%')->where('price', 'BETWEEN', $min_price, 'AND', $max_price)->get() as $search_results)

回答by Stuart Wagner

You're looking for whereBetween(). First, put the Eloquent query in your controller:

您正在寻找whereBetween(). 首先,将 Eloquent 查询放在您的控制器中:

$inventory = Inventory::where('category', $cat)
    ->where('title', 'like', '%' . $search_query . '%')
    ->whereBetween('price', [$min_price, $max_price])
    ->get();

Then, pass the $inventoryvariable to your view:

然后,将$inventory变量传递给您的视图:

return view('my.view.path', compact('inventory'));

Finally, in your view, you can loop through the results:

最后,在您看来,您可以遍历结果:

@foreach($inventory as $search_results)

See the Laravel Docsfor more info.

有关更多信息,请参阅Laravel 文档