Auto-complete text field in laravel using database

I am trying to make an autocomplete form as shown below, but the form does not display a suggestion, since my database query is in order.

enter image description here

Cole shape:

enter image description here

Controller Method Code:

enter image description here

Routes

enter image description here

When I search for a link, I get the result of the query as follows:

enter image description here

Shows the result:

enter image description here

[{"id": 1, "value": "sourav hossen"}, {"id": 2, "value": "sourav hossen"}, {"id": 3, "value": "sourav hossen" }, {"id": 4, "value": "ab"}, {"id": 5, "value": "aa"}]

+5
source share
2 answers

Try this change, it will work for a while.

source: "{{URL::route('autocomplete')}}", 
0
source

I tried to do this using jquery ajax and it worked.
First of all, you must include the jquery library before the following code.

Your view should have javascript code:

 <script> $(document).ready(function(){ $('#q').keyup(function () { var q=$(this).val(); if(word.length>3) { $.ajax ({ type: "GET", url: "test2", data: {q:q}, contentType: "json", cache: false, success: function(data, status, xhr) { $('#q').val(data[0].value); } }); } }); }); </script> 

In your controller you should get ajax data

 public function autocomplete(Request $request) { $input = $request->all(); $term = $input['q']; $result = array(); $queries = ...(do whatever you like) ->take(5)->get(); foreach($queries as $query) { $result[] = ['id'=> $query->id,'value'=>$query->firstname.' '.$query->lastname]; } return response()->json($result); } 

Try this, and if you find any difficulties, I will be here.

0
source

Source: https://habr.com/ru/post/1211576/


All Articles