Last active
April 4, 2024 13:15
-
-
Save imranismail/10200241 to your computer and use it in GitHub Desktop.
Laravel And JqueryUI's Autocomplete Plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I noticed that your web page does not include the JS library from jQuery Ui's CSS and JS library.
Firstly, include the following libaries into your web pages:
Checkout this guide: https://www.tutsmake.com/laravel-jquery-ui-autocomplete-ajax-search-example/