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.
source share