I remember to have changed to that but it did not work.
But I followed Jeffrey's tutorial and just trying to implement the way he explained.
All works, but only if I type the query in the url my self.
If I user the form, it gives me this error:
Too few arguments to function App\PropertyFilters::min_price(), 0 passed and exactly 1 expected
And the url converts into these:
http://owlcrest.local/search?city_suburb=&type=apartment&min_price=&max_price=&bedrooms=&bathrooms=
Which seems like all records are compulsory
My form looks like this:
{!! Form::open(['method' => 'GET', 'action'=>'AdminPropertyController@search_page']) !!}
<div class="row flex wrap">
<div class="col-sm-10">
<div class="search-input">
{!! Form::text('city_suburb', null, ['class'=>'form-controla', 'placeholder'=>'Search for suburb']) !!}
</div>
<div class="select-boxes">
{!! Form::select('type', [''=>'Type of property'] + ['apartment'=>'Apartment', 'house'=>'House', 'plot'=>'Plot', 'smallholding'=>'Smallholding', 'commercial'=>'Commercial', 'townhouse'=>'Townhouse'], null, ['class'=>'form-']) !!}
{!! Form::select('min_price', [''=>'Minimum'] + [450000 => 'R 450 000', 5000000 => 'R 500 000', 1000000 => 'R 1 000 000',2000000 => 'R 2 000 000', 3000000 => 'R 3 000 000', 4000000 => 'R 4 000 000',5000000 => 'R 5 000 000', 6000000 => 'R 6 000 000',
], null, ['class'=>'form-']) !!}
{!! Form::select('max_price', [''=>'Maximum'] + [450000 => 'R 450 000', 500000 => 'R 500 000', 1000000 => 'R 1 000 000',2000000 => 'R 2 000 000', 3000000 => 'R 3 000 000', 4000000 => 'R 4 000 000',5000000 => 'R 5 000 000', 6000000 => 'R 6 000 000',
], null, ['class'=>'form-']) !!}
{!! Form::select('bedrooms', [''=>'Bedroom'] + [2=>'1+', 3=>'2+', 20=>'20+'], null, ['class'=>'form-']) !!}
{!! Form::select('bathrooms', [''=>'bathroom'] + [2=>'1+', 3=>'2+', 20=>'20+'], null, ['class'=>'form-']) !!}
<div class="col-sm-2">
{!! Form::submit('Search', ['class'=>'search bg-brown']) !!}
<button class="clear-filter button">Clear Filter</button>
</div>
</div>
{!! Form::close() !!}
My function in the search_page became like the one below:
public function search_page(PropertyFilters $filters)
{
return Property::filter($filters)->get();
}
PropertyFilters.php
class PropertyFilters extends QueryFilter
{
public function type($type){
return $this->builder->where('type', $type);
}
public function min_price($price){
return $this->builder->where('price', '>=', $price);
}
public function max_price($price){
return $this->builder->where('price', '<=', $price);
}
public function bathrooms($bathrooms){
return $this->builder->where('bathrooms', $bathrooms);
}
public function bedrooms($bedrooms){
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(method_exists($this, $name)){
call_user_func_array([$this, $name], array_filter([$value]));
}
}
return $this->builder;
}
public function filters(){
return $this->request->all();
}
}
I guess this goes back to the question @Snapey asked previously:
When a user enters different searches, are you wanting them to be AND (the reply must contain all the search terms) or OR (the reply contains any of the search terms).
So in this case the reply should only contain the the search terms.