Last active
February 26, 2018 03:40
-
-
Save jeffcdavis/6abca56d3ea90103bdeb26195211c41b to your computer and use it in GitHub Desktop.
Laravel Registration Autocomplete text field
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
$('#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); | |
} | |
}); | |
}); |
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
<input class="form-control" placeholder="Start typing..." type="text" name="term" id="q" data-action="{{ route('search-autocomplete') }}"> |
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
<?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); | |
} | |
} |
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
<?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