我在Laravel中为广告/属性构建了一个应用程序。我有一个带有复选框的过滤器的搜索表单。当我从同一个请求中选择两个选项时遇到问题,例如供需,需求为PropertyBidAsk
,我只能从需求中得到结果,而不能同时从两者中得到结果,而且我想在表单提交后同时检查两个结果。供求是类别表中类别列中的值。任何帮助表示赞赏。这是我的代码。
CategoryController.php:
public function search(Request $request, Property $property)
{
$category = $property->category;
$query = Property::query();
if ($request->has('propertyBidAsk')) {
$request->get('propertyBidAsk');
}
if ($request->propertyBidAsK == 'supply' && $request->propertyBidAsk== 'demand') {
$query->whereHas('category', function ($query) use ($request) {
$query->where('category', 'like', '%' . $request->propertyBidAsk . '%');
});
} elseif ($request->propertyBidAsk == 'supply') {
$query->whereHas('category', function ($query) use ($request) {
$query->where('category', 'like', '%' . 'supply' . '%');
});
} else if ($request->propertyBidAsk == 'demand') {
$query->whereHas('category', function ($query) use ($request) {
$query->where('category', 'like', '%' . 'demand' . '%');
});
}
$results = $query->paginate(6);
return view('search', compact('category', 'results', 'request'));
}
search.blade.php:
<div class="col-md-2 mb-6">
<h5>Payment Method</h4>
<div class="d-block my-3">
<div class="custom-control custom-checkbox">
<input id="supply" name="propertyBidAsk" value="supply" type="checkbox" class="custom-control-input" @if(old('propertyBidAsk', $request->propertyBidAsk ?? 'default') === 'supply') checked @endif>
<label class="custom-control-label" for="supply">supply</label>
</div>
<div class="custom-control custom-checkbox">
<input id="demand" name="propertyBidAsk" value="demand" type="checkbox" class="custom-control-input" @if(old('propertyBidAsk', $request->propertyBidAsk ?? 'default') === 'demand') checked @endif>
<label class="custom-control-label" for="demand">demand</label>
</div>
</div>
</div>
答案 0 :(得分:1)
要获得具有相同名称attr的多选输出,请将复选框输入的名称更新为array作为propertyBidAsk []。您将获得输入数组。
<input id="supply" name="propertyBidAsk[]" value="supply" type="checkbox" class="custom-control-input" @if(old('propertyBidAsk', $request->propertyBidAsk ?? 'default') === 'supply') checked @endif>
<input id="demand" name="propertyBidAsk[]" value="demand" type="checkbox" class="custom-control-input" @if(old('propertyBidAsk', $request->propertyBidAsk ?? 'default') === 'demand') checked @endif>
在控制器中将获得该值。
public function search(Request $request, Property $property)
{
$category = $property->category;
$query = Property::query();
if ($request->has('propertyBidAsk')) {
$request->get('propertyBidAsk');
}
$propertyBidAsk = $request->input('propertyBidAsk'); //$propertyBidAsk will get in array
if (in_array('supply', $propertyBidAsk) && in_array('demand', $propertyBidAsk)){
$query->whereHas('category', function ($query) use ($request) {
$query->where('category', 'like', '%demand%')->orWhere('category', 'like', '%supply%')
});
} else if(in_array('supply', $propertyBidAsk)){
$query->whereHas('category', function ($query) use ($request) {
$query->where('category', 'like', '%' . 'supply' . '%');
});
} else if(in_array('demand', $propertyBidAsk)){
$query->whereHas('category', function ($query) use ($request) {
$query->where('category', 'like', '%' . 'demand' . '%');
});
}
$results = $query->paginate(6);
return view('search', compact('category', 'results', 'request'));
}
答案 1 :(得分:1)
@Shivendra Singh回答,您可以做到这一点,您也可以这样做
<input id="supply" name="propertyBidAsk1" value="supply" type="checkbox" class="custom-control-input" @if(old('propertyBidAsk', $request->propertyBidAsk ?? 'default') === 'supply') checked @endif>
<input id="demand" name="propertyBidAsk2" value="demand" type="checkbox" class="custom-control-input" @if(old('propertyBidAsk', $request->propertyBidAsk ?? 'default') === 'demand') checked @endif>
您会发现表单元素中的名称不能相同,它将始终获得第一个值。
然后在您的控制器中
if (!empty($request->propertyBidAsK1) && !empty($request->propertyBidAsk2) {
$query->whereHas('category', function ($query) use ($request) {
$query->where('category', 'like', '%' . $request->propertyBidAsk1 . '%')
->orWhere('category', 'like', '%' . $request->propertyBidAsk2 . '%');
});
} elseif (!empty($request->propertyBidAsk1)) {
$query->whereHas('category', function ($query) use ($request) {
$query->where('category', 'like', '%' . 'supply' . '%');
});
} else if (!empty($request->propertyBidAsk2)) {
$query->whereHas('category', function ($query) use ($request) {
$query->where('category', 'like', '%' . 'demand' . '%');
});
}
希望有帮助