I have a context where there is a conferences table that stores the conferences. The conference table has a column "name" and also a column city".
I have an input type text and the user should be able to search for conference names but also for conferences that will happen in a specific city.
To explain better: The user enters "Ne" in the input text. So it should appear the results relative to conference names but also city names. If the user clicks in the name of a conference he should be redirected to the conference details page. If he clicks in a city should appear the conferences that have the clicked city as a value in the column "city" of the conferences table.
Do you know how this can be achieved? For now, is just working the part of appearing the conferences in the input search.
I created an AutocompleteController for this context:
class AutocompleteController extends Controller
{
public function search(Request $request){
$search = $request->term;
$conferences = Conference::where('name', 'LIKE', '%'.$search.'%')->get();
$data= [];
foreach ($conferences as $key => $value){
$data[] = ['id'=> $value->id, 'value' => $value->name . " " . $value->name];
}
return response($data);
}
}
Route to above method:
Route::get('/autocomplete-search', 'AutocompleteController@search');
jQuery:
$("#search").autocomplete({
source: "{{ URL::to('autocomplete-search') }}",
minLength: 2,
select:function(key, value)
{
console.log(value);
}
});
Search input:
<div class="col col-md-6">
<h4 class="text-white text-center font-weight-bold">Search</h4>
<form class="main-search">
<input type="text" id="search" class="autocomplete dropdown-toggle" placeholder="Conference, Citiy, Category">
</form>
</div>
Conferences table:
conferences table
id | name | city | ...
1 | New conference 1 | Newcastle
2 | New conference 2 | Newcastle
3 | old conference 1 | Bristol
4 | old conference 2 | Glasgow