I added subcategories to my app with help of here Adding subcategories to laravel 5.4 and before that my search base on category
worked just fine now i try to change my search base on subcategory
and it doesn't return results.
here is my search function:
public function search()
{
$searchTitle = request('title');
$searchCategory = request('subcategory');
$searchLocation = request('location');
$ads = null;
if($searchTitle || $searchCategory || $searchLocation) {
$ads = Ad::when($searchTitle, function ($query) use ($searchTitle) {
return $query->where('title', 'like', "%{$searchTitle}%")
->orWhere('description', 'like', "%{$searchTitle}%");
})
->when($searchCategory, function ($query) use ($searchCategory) {
return $query->whereHas('subcategory', function ($query) use ($searchCategory) {
$query->where('id', $searchCategory);
});
})
->when($searchLocation, function ($query) use ($searchLocation) {
return $query->whereHas('location', function ($query) use ($searchLocation) {
$query->where('id', $searchLocation);
});
})
->paginate(10)
->appends(request()->query());
}
return view('search-result', compact('ads'));
}
this is my search form category part:
<label data-toggle="tooltip" data-placement="top" title="select category" class="sr-only mr-sm-2" for="inlineFormCustomSelect">Category</label>
<select name="subcategory_id" data-toggle="tooltip" data-placement="top" title="select category"
class="custom-select mb-2 mr-sm-2 mb-sm-0" id="inlineFormCustomSelect">
<option value="">Select category</option>
@foreach ($categories as $category)
<optgroup label="{{ $category->name }}">
@foreach($category->subcategories as $sub)
<option value="{{ $sub->id }}">{{ $sub->name }}</option>
@endforeach
</optgroup>
@endforeach
</select>
I tried many changes and played with mu function code but nothing still will return no results!
error.