使用Laravel 5.2自动完成 [英] Auto Complete with Laravel 5.2
问题描述
我的网站上已有功能搜索框,但是我想为搜索栏实现自动完成功能.我知道有很多插件,例如typeahead等.我想实现typeahead或 jQuery Autocomplete小部件,如果你们知道的话.我看过一些教程,但是它们不适用于我拥有的代码.我将为您显示搜索框所需的刀片,控制器和路由.
I already have a functional search box form my website, but i want to implement a auto complete functionality for my search bar. I know there are a bunch of plugins like typeahead, and etc. I would like to implement typeahead or the jQuery Autocomplete widget, if you guys know how. I seen some tutorials, but they don't work for the code i have. I will show you the blade, controller and route that I have for my search box.
如何将我的数据库中的所有数据加载到jQuery插件的source字段中?我想加载传单标题吗?
How can I load all my data from my database into the source field in the jQuery plugin? I would want to load a flyers title for example?
show.blade.php:
show.blade.php:
@extends('home')
@section('content')
<div id="the-basics">
<input class="typeahead" type="text" placeholder="Title">
</div>
@stop
@section('scripts.footer')
<script type="application/javascript" src="{{ URL::asset('/src/public/js/typeahead.js') }}"></script>
<script>
$('#the-basics .typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
name: 'title',
source: 'travelflyers/search'
});
</script>
@stop
TravelFlyersController.php:
TravelFlyersController.php:
class TravelFlyersController extends Controller {
// Other functions here...
public function search() {
$keyword = Input::get('keyword');
$flyers = Flyer::where('title', 'LIKE', '%' .$keyword. '%')->get();
return \Response::json($flyers);
}
}
路线:
Route::group(['middleware' => ['web']], function () {
/** Resource Route For Travel Flyers */
Route::resource('travelflyers', 'TravelFlyersController');
Route::post('travelflyers/search',[
'uses' => '\App\Http\Controllers\TravelFlyersController@search',
'as' => 'travelflyers.search',
]);
});
推荐答案
在路由文件中尝试使用GET
代替POST
Route::get('travelflyers/search',[
'uses' => '\App\Http\Controllers\TravelFlyersController@search',
'as' => 'travelflyers.search',
]);
这篇关于使用Laravel 5.2自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!