dimanche 19 juin 2016

Laravel 5 autocomplete not working fine

I've been working to add autocomplete to my Location text field, the problem is when I type any word on it, it always display all the result.

This is my javascript:

    <script type="text/javascript">
    $(function(){
        $("#Location").autocomplete({
            source: "",
            minLength: 3,
            select: function(event, ui){
                $("#Location").val(ui.item.value);
             }
        });
    });
</script>

This is the route.php

Route::get('search/autocomplete', ['uses' => 'SearchController@autocomplete', 'as' => 'search.autocomplete']);

This is the searchController

    public function autocomplete(Request $request){
        $term = $request->get('Location');

        $provinces = DB::table('provinces')->where('ProvinceName', 'LIKE', '%'. $term .'%')
        ->orderBy('ProvinceName', 'desc')
        ->get();

        $results = [];
        foreach ($provinces as $province) {
            $results[] = ['id' => $province->id, 'value' => $province->ProvinceName];
        }
        return response()->json($results);
}

and this is the search.blade.php

<div class="form-group col-md-4">
                Location: {!!Form::text('Location', Request::get('Location'), ['class' => 'form-control', 'placeholder' => 'What is your location?', 'id' => 'Location', 'style' => 'width: 250px;'])!!}
</div>



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire