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

Bra*_*don 1 php mysql laravel eloquent laravel-4

我正在尝试运行一个雄辩的查询来按类别,然后是标题,然后是价格来过滤结果.类别和标题的工作原理应该如此,但是当我添加BETWEEN以便在两个价格之间进行搜索时,它就破了.有谁知道我如何解决我的疑问?

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

Stu*_*ner 6

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

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

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

return view('my.view.path', compact('inventory'));
Run Code Online (Sandbox Code Playgroud)

最后,在您的视图中,您可以遍历结果:

@foreach($inventory as $search_results)
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅Laravel文档.