I building an application for ads/properties in Laravel. I have a search form with filters that are checkboxes. I am having a problem when I select two options from the same request for example supply, demand that are PropertyBidAsk
I get only results from demand and not both, also I would like to keep both checked after form submission. Supply and demand are values in the category column in the categories table. Any help is appreciated. Here is my code.
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>