Could you post the full url you are using and/or any post data you are sending in the request
Dec 3, 2018
14
Level 5
Search query not working with min and max values
I have form with multiple search input fields, which min and max values are included. When I try to search all properties which min price is 450 000 I see nothing, but in my database I have properties that are 2 000 000. Also on the same note, If put 1 000 000 as max value It shows me properties that are even 2 000 000.
My search function in the controller:
public function search_page(PropertyFilters $filters)
{
$properties = Property::filter($filters)->paginate(9);
return view('pages.search', compact('properties'));
}
PropertyFilters.php:
public function city_suburb($city){
if(!$city) {
return $this->builder;
}
return $this->builder->where('city', $city)->orWhere('suburb', $city);
}
public function type($type){
if(!$type) {
return $this->builder;
}
return $this->builder->where('type', $type);
}
public function min_price($price){
if(!$price) {
return $this->builder;
}
return $this->builder->where('price', '>=', $price);
}
public function max_price($price){
if(!$price) {
return $this->builder;
}
return $this->builder->where('price', '<=', $price);
}
public function bathrooms($bathrooms){
if(!$bathrooms) {
return $this->builder;
}
return $this->builder->where('bathrooms', $bathrooms);
}
public function bedrooms($bedrooms){
if(!$bedrooms) {
return $this->builder;
}
return $this->builder->where('bedrooms', $bedrooms);
}
QueryFilter.php
abstract class QueryFilter
{
protected $request;
protected $builder;
public function __construct(Request $request){
$this->request = $request;
}
public function apply(Builder $builder){
$this->builder = $builder;
foreach($this->filters() as $name => $value){
if($value && method_exists($this, $name)){
call_user_func_array([$this, $name], array_filter([$value]));
}
}
return $this->builder;
}
public function filters(){
return $this->request->all();
}
}
Anything possibly done wrong in the logic?
Please or to participate in this conversation.