简体   繁体   中英

laravel 5 : Autocomplete Search

I'm trying to make a autocomplete search in my laravel application. But I'm facing a error while I search for the keyword. I got the following error while i see in console :

http://localhost/autocomplete?term=a 404 (Not Found)

Here is the route I have used:

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

Here is the controller:

public function autocomplete(Request $request)
{
  $results=array();
    $item = $request->input(['searchname']);
    $data=User::where('name','LIKE','%' .$item.'%')
    ->take(5)
    ->get();
    foreach ($data as $data) {
      $results[]=['id'=>$data->id,'value'=>$data->name];
    }
return response()->json($results);
}

And here is the view page with the javascript code:

<div class="ui-widget">
    <input type="text" name="searchname" id="searchname" placeholder="Search" class="form-control">
  </div>

 <script>
   jQuery(document).ready(function($) {
      $('#searchname').autocomplete({
            source:  "/autocomplete" ,
            minlength:1,
            autofocus:true,
            select:function(event,ui){
              $("searchname").val(ui.item.value);
            }
      });
   });
    </script>

If anyone find what's the error, try to provide the solution please.Thanks in Advance!

There's a typo here

public function autocomplte(Request $request)

It should be

public function autocomplete(Request $request)

是否在JQuery库之后加载了JQueryui脚本?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM