Skip to content

Instantly share code, notes, and snippets.

@JspDarker
Forked from imranismail/autocomplete.php
Created June 12, 2018 02:58
Show Gist options
  • Select an option

  • Save JspDarker/87cd9b4591e9b4c6a0fbbefc8e1ef218 to your computer and use it in GitHub Desktop.

Select an option

Save JspDarker/87cd9b4591e9b4c6a0fbbefc8e1ef218 to your computer and use it in GitHub Desktop.

Revisions

  1. @imranismail imranismail revised this gist Apr 8, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion autocomplete.php
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@ public function autocomplete(){

    //View
    {{ Form::open(['action' => ['SearchController@searchUser'], 'method' => 'GET']) }}
    {{ Form::text('q', '', ['id' => 'q', 'placeholder' => 'Enter first name'])}}
    {{ Form::text('q', '', ['id' => 'q', 'placeholder' => 'Enter name'])}}
    {{ Form::submit('Search', array('class' => 'button expand')) }}
    {{ Form::close() }}

  2. @imranismail imranismail revised this gist Apr 8, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion autocomplete.php
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@ public function autocomplete(){

    //View
    {{ Form::open(['action' => ['SearchController@searchUser'], 'method' => 'GET']) }}
    {{ Form::text('q', '', ['id' => 'q', 'name' => 'q', 'placeholder' => 'Enter first name'])}}
    {{ Form::text('q', '', ['id' => 'q', 'placeholder' => 'Enter first name'])}}
    {{ Form::submit('Search', array('class' => 'button expand')) }}
    {{ Form::close() }}

  3. @imranismail imranismail created this gist Apr 8, 2014.
    41 changes: 41 additions & 0 deletions autocomplete.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    //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', 'name' => 'q', 'placeholder' => 'Enter first 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);
    }
    });
    });