Skip to content

Instantly share code, notes, and snippets.

@manoj-nandakumar
Forked from imranismail/autocomplete.php
Created February 21, 2016 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manoj-nandakumar/dfd5d3b456b1f7dbe608 to your computer and use it in GitHub Desktop.
Save manoj-nandakumar/dfd5d3b456b1f7dbe608 to your computer and use it in GitHub Desktop.
Laravel And JqueryUI's Autocomplete Plugin
//SearchController.php
public function autocomplete(){
$term = Input::get('term');
$results = array();
$queries = DB::table('users')
->where('first_name', 'LIKE', '%'.$term.'%')
->orWhere('last_name', 'LIKE', '%'.$term.'%')
->take(5)->get();
foreach ($queries as $query)
{
$results[] = [ 'id' => $query->id, 'value' => $query->first_name.' '.$query->last_name ];
}
return Response::json($results);
}
//View
{{ Form::open(['action' => ['SearchController@searchUser'], 'method' => 'GET']) }}
{{ Form::text('q', '', ['id' => 'q', 'placeholder' => 'Enter name'])}}
{{ Form::submit('Search', array('class' => 'button expand')) }}
{{ Form::close() }}
//Route
Route::get('search/autocomplete', 'SearchController@autocomplete');
//Javascript
$(function()
{
$( "#q" ).autocomplete({
source: "search/autocomplete",
minLength: 3,
select: function(event, ui) {
$('#q').val(ui.item.value);
}
});
});
@nouart
Copy link

nouart commented Feb 23, 2016

I use the same code of you but i can't display data , please see the fig
what is the solution in your mind ?
autocomplete

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment