我想使用数据库中的现有标题对输入内容执行自动完成功能,但似乎不起作用。我不知道出了什么问题,但是当我尝试写点东西时,便注意到了。
这是我的控制人
public function search()
{
return view('search-me');
}
public function autocomplete(Request $request)
{
$data = Models::select("email")->where("email", "LIKE","%{$request->input("query")}%")->get();
return response()->json($data);
}
这是我的路线
Route::get('search-me', array('as' => 'search', 'uses' => 'AdminNewsController@search'));
Route::get('autocomplete',array('as' => 'autocomplete', 'uses' => 'AdminNewsController@autocomplete'));
这是我的观点
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" />
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.4.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-3-typeahead/4.0.2/bootstrap3-typeahead.js"></script>
</head>
<body>
<div class="container">
<h1> test</h1>
<input type="text" class="typeahead form-control">
</div>
</body>
<script type="text/javascript">
var path = "{{ route('autocomplete') }}";
$('input.typeahead').typeahead({
source: function (query, process){
return $.get(path, { query: query}, function (data) {
return process(data);
});
}
});
</script>
</html>
我正在使用Laravel 5.2,但我想也在开发我的。这是本教程:https://www.youtube.com/watch?v=3AiMsvobceY