Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching between two numbers(prices) in Laravel

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?

@foreach(Inventory::where('category', '=', $cat)->where('title', 'LIKE', '%'. $search_query .'%')->where('price', 'BETWEEN', $min_price, 'AND', $max_price)->get() as $search_results)
like image 211
Brandon Avatar asked Aug 12 '15 20:08

Brandon


1 Answers

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

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

Then, pass the $inventory variable to your view:

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 Docs for more info.

like image 123
Stuart Wagner Avatar answered Sep 19 '22 17:09

Stuart Wagner