Skip to content

Instantly share code, notes, and snippets.

@jeffcdavis
Last active February 26, 2018 03:40
Show Gist options
  • Save jeffcdavis/6abca56d3ea90103bdeb26195211c41b to your computer and use it in GitHub Desktop.
Save jeffcdavis/6abca56d3ea90103bdeb26195211c41b to your computer and use it in GitHub Desktop.
Laravel Registration Autocomplete text field
$('#q').each(function() {
var $this = $(this);
var src = $this.data('action');
$this.autocomplete({
source: src,
minLength: 2,
select: function(event, ui) {
$this.val(ui.item.value);
$('#id').val(ui.item.id);
}
});
});
<input class="form-control" placeholder="Start typing..." type="text" name="term" id="q" data-action="{{ route('search-autocomplete') }}">
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Center;
class SearchController extends Controller {
public function autocomplete() {
$term = request('term');
$result = Center::whereName($term)->orWhere('name', 'LIKE', '%' . $term . '%')->get(['id', 'name as value']);
return response()->json($result);
}
}
<?php
Route::get('search/autocomplete', ['as' => 'search-autocomplete', 'uses' => 'SearchController@autocomplete']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment