I want to be able to search user by the category to which they belong and also by the area in which they are located.
I have 4 tables for that which include,
A User table with id, name, surname, email, phone, type, photo and area_id.
A category table with id, name, description
A Service table with id, name, description, and category_id.
and I have a Pivot table between Service and User named service_user with attributes id, name, price, description, user_id, and service_id.
I wish to know how can I search users by their category and area, provided all the information exists in the database using Laravel. I have managed to do a search by area alone and it works. Just now remaining the category which I don't know why it's not working or what exactly to do since in my database the category id migrates to the service table, also the service_id migrates to the pivot table(service_user) alongside with the user_id. So it's a many to many relationship btw users and services.
In my web file, I have this
Route::get('/search', 'UserController@search')->name('search');
In my user Controller, I did something like this
public function search(Request $request) {
$category = $request->get('category');
$area = Input::get('area');
//dd($category);
//dd($area);
// $p = $town->id;
// $v = Input::get('town.$town->id');
//$va = $request->getPathInfo();
if(!(empty($category)) && !(empty($area))) {
$results = User::with(['area', 'services', 'category'])
->where('services.category.category_id', 'like', "%$category%")
->where('area_id', 'like', "%$area%")
->get();
//Section::inject('title','Search');
// dd($results);
return view('Search.search', compact('results'));
}
elseif (!(empty($category)) && empty($area)) {
$results = User::with(['area', 'services'])
->where('services.category.category_id', 'like', "%$category%")
->get();
//dd($results);
return view('Search.search', compact('results'));
}
elseif(empty($category) && !empty($area)) {
$results = User::with(['area', 'services'])
->where('area_id', 'like', "%$area%")
->get();
//dd($results);
return view('Search.search', compact('results'));
}
return view('Users/jobberlist');
}
and in my Home page with the search form, I did this
<form action= "{{ route('search') }}" method="GET">
<div class="row">
<div class="col-lg-7 col-md-5 col-sm-5 col-xs-12">
<div class="job-field">
<select name="category">
<option value="">Je recherche une personne pour...</option>
@foreach ($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
@endforeach
</select>
</div>
</div>
<div class="col-lg-4 col-md-5 col-sm-5 col-xs-12">
<div class="job-field">
<select data-placeholder="City, province or region" class="chosen-city" name="area">
<option value="">Choissisez un quartier...</option>
@foreach ($areas as $area)
<option value="{{ $area->id }}">{{ $area->name }}</option>
@endforeach
</select>
<i class="la la-map-marker"></i>
</div>
</div>
<div class="col-lg-1 col-md-2 col-sm-2 col-xs-12">
<button type="submit"><i class="la la-search"></i></button>
</div>
</div>
</form>
Can someone help me out to solve this problem? I will gladly appreciate.
Something like this might work for you