I have search form to list properties/ads through certain criteria. In my form I am trying to search and list properties from database that correspond to entered min_price and max_price. When I submit form I get no results in table and when I die and dump min_price or max_price variable I get false. Any help is appreciated. Here is my code
CategoryController.php
<?php
namespace App\Http\Controllers;
use App\Category;
use App\Property;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
class CategoryController extends Controller
{
public function index()
{
return view('categories.search', compact('data'));
}
public function search($price, Request $request, Property $property)
{
$category = $property->category;
$query = Property::query();
// Code for min and max price
$min_price = $request->has('min_price');
$max_price = $request->has('max_price');
//dd($max_price);
if (($min_price) && ($max_price)) {
$query->whereBetween('price', [$min_price, $max_price]);
}
elseif (! is_null($min_price)) {
$query->where('price', '>=', $min_price);
}
elseif (! is_null($max_price)) {
$query->where('price', '<=', $max_price);
}
$results = $query->get();
return view('categories.search', compact('category', 'results'));
}
}
search.blade.php
@if(isset($results))
<table class="table">
<thead>
<th>Price</th>
</thead>
<tbody>
@foreach ($results as $result)
<tr>
<td>{{ $result->price }}</td>
</tr>
@endforeach
</tbody>
</table>
@endif
<form id="searchForm" method="GET" action="/search">
<div class="col-md-6 mb-6">
<label>Price</label>
<input type="number" id="min_price" name="min_price" class="form-control" placeholder="Min Price">
<input type="number" id="max_price" name="max_price" class="form-control" placeholder="Max Price">
</div>
<button class="btn btn-primary btn-lg btn-block">Search</button>
</form>