大家好:我有点固执己见。
问题:我正在尝试使用html表单为“属性”模型构建搜索功能。有几个过滤器,例如price
,property_type
等。 Property.php
的位置是主要过滤器之一; property_type
可以是warehouse
,house
,apartment
。这是主要问题; house
属于Neighborhood.php
,但是apartment
属于Building.php
,而Building.php
属于Neighborhood
。
我就是这样
我的模特
// Property.php
class Property extends Model{
public function building()
{
return $this->belongsTo('App\Building');
}
public function neighborhood()
{
return $this->belongsTo('App\Neighborhood');
}
}
<!-- The search form -->
<form method="GET" action="{{route('property.search')}}">
{{ csrf_field() }}
<select class="form-control" name="city_id" id="city" data-required>
<option value="">Select a state</option>
@foreach($cities as $city)
<option value="{{ $city->id }}"{{ old('city_id') == $city->id ? ' selected' : '' }}>{{ $city->name }}</option>
@endforeach
</select>
<select class="form-control property_type" name="property_type"
id="property_type"
data-required>
<option value="">Select an option</option>
<option value="house" {{ old('property_type') == 'house' ? ' selected' : '' }}>House</option>
<option value="apartament" {{ old('property_type') == 'apartament'? ' selected' : '' }}>Apartament
</select>
<!-- some other filters -->
<input type="checkbox" id="sell" name="sell"><label>Venta</label>
<input type="checkbox" id="rent" name="rent"><label>Arriendo</label>
<button type="submit" class="btn btn-primary">Buscar</button>
</form>
// My PropertyController
public function search(PropertySearchRequest $request)
{
$data['cities'] = City::all();
$prices = explode(",", $request->price);
/*Obtener por rango de precio*/
$properties = Property::where('price', '>=', $prices[0])
->where('price', '<=', $prices[1])
->where('property_type', $request->property_type);
if($request->rent == 'on' && $request->sell == 'on'){
$data['properties'] = $properties->paginate(8);
return view('properties.search', $data);
}
if($request->rent == 'on'){
$propiedades = $properties->where('rent', true);
$data['properties'] = $properties->paginate(8);
return view('properties.search', $data);
}
if($request->sell == 'on'){
$properties = $properties->where('sell', true);
$data['properties'] = $properties->paginate(8);
return view('properties.search', $data);
}
}
如您所见,我可以过滤除该身份所属城市以外的所有内容
问题:如何查看并在视图中分页结果?
第一种方法:
$properties = $properties->filter(function ($property, $key) use ($request) {
if ($property->building_id) {
return $propiedad->building()->first()->neighborhood()->first()->city()->first()->id == $request->city_id;
} else {
return $propiedad->neighborhood()->first()->city()->first()->id == $request->city_id;
}
});
但是我无法对此进行分页
有人可以帮助我吗?