I already have a functional search box form my website, but i want to implement a auto complete functionality for my search bar. I know there are a bunch of plugins like typeahead, and etc. I would like to implement typeahead or the jQuery Autocomplete widget, if you guys know how. I seen some tutorials, but they don't work for the code i have. I will show you the blade, controller and route that I have for my search box.
How can I load all my data from my database into the source field in the jQuery plugin? I would want to load a flyers title for example?
show.blade.php:
@extends('home')
@section('content')
<div id="the-basics">
<input class="typeahead" type="text" placeholder="Title">
</div>
@stop
@section('scripts.footer')
<script type="application/javascript" src="{{ URL::asset('/src/public/js/typeahead.js') }}"></script>
<script>
$('#the-basics .typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
name: 'title',
source: 'travelflyers/search'
});
</script>
@stop
TravelFlyersController.php:
class TravelFlyersController extends Controller {
// Other functions here...
public function search() {
$keyword = Input::get('keyword');
$flyers = Flyer::where('title', 'LIKE', '%' .$keyword. '%')->get();
return \Response::json($flyers);
}
}
Route:
Route::group(['middleware' => ['web']], function () {
/** Resource Route For Travel Flyers */
Route::resource('travelflyers', 'TravelFlyersController');
Route::post('travelflyers/search',[
'uses' => '\App\Http\Controllers\TravelFlyersController@search',
'as' => 'travelflyers.search',
]);
});