我有一个包含多个搜索输入字段的表单,其中包含最小值和最大值。当我尝试搜索所有最小价格为450 000的属性时,我什么也看不到,但是在我的数据库中,我拥有的属性为2000000。 同样要注意的是,如果将1000000作为最大值,则显示的属性甚至为2000000。
刀片模板:
{!! 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 none">
{!! 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>
</div>
<div class="col-sm-2">
{!! Form::submit('Search', ['class'=>'search bg-brown']) !!}
<button class="clear-filter button">Clear Filter</button>
</div>
</div>
{!! Form::close() !!}
我在控制器中的搜索功能:
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();
}
}
下面的所有代码都在我的Property.php中
protected $fillable = [
];
protected $uploads = '/images/';
public function getFeaturedImageAttribute($path) {
if ( $path ) {
return $this->uploads.$path;
}
}
public function photoPlaceholder(){
return "https://ibin.co/4C4hfps5SKiw.jpg";
}
public function user(){
return $this->belongsTo('App\User');
}
public function photos(){
return $this->hasMany('App\Photo');
}
public function offers(){
return $this->hasMany('App\Offer');
}
public function feature(){
return $this->hasOne('App\Feature');
}
public function viewing(){
return $this->hasMany('App\Viewing');
}
public function viewingdate(){
return $this->hasMany('App\Viewingdate');
}
public function scopeFilter($query, QueryFilter $filters){
return $filters->apply($query);
}
public function agent(){
return $this->hasOne('App\Agent');
}
逻辑上可能做错了什么?