我一直在嘗試爲網頁進行實時搜索輸入以搜索數據庫中的數據。到目前爲止,我想出了這一點:Laravel jQuery AJAX Live搜索
控制器:
public function launchsitefilter($site_code, Request $request) {
$launchsites = DB::table('launchsites')->where('site_code', $site_code)->get();
if(! $launchsites){
return abort(404);
}
$launchsitesatellite = DB::table('satellites')->where('site', $site_code)->get();
if ($request->ajax())
{
$output="";
$launchsitesatellite = DB::table('satellites')->where('satname','LIKE','%'.$request->search.'%')
->orWhere('norad_cat_id','LIKE','%'.$request->search.'%');
if ($launchsitesatellite)
{
foreach ($launchsitesatellite as $key => $launchsitesatellites) {
$output ='<tr>'.
'<td>'. $launchsitesatellites->satname .'</td>'.
'<td>'.$launchsitesatellites->norad_cat_id.'</td>'.
'<td>'.$launchsitesatellites->object_type.'</td>'.
'</tr>';
}
}
}
return view('pages/launchsite-filter', compact('launchsites', 'launchsitesatellite'));
}
腳本:
$('#search').on('keyup', function() {
$value=$(this).val();
$.ajax({
type : 'get',
url : '{{$launchsitename->site_code}}',
data : {'search':$value},
success:function(data) {
console.log(data);
}
});
});
刀片:
<input type="text" class="form-control" id="search" name="search"></input>
當我輸入一些東西到輸入我在控制檯收到此錯誤:
GET http://space-observe.dev/launch-site/JSC?search=search term 500 (Internal Server Error)
我猜的JavaScript是工作時存在在我的控制器錯誤。您需要做些什麼才能使實時搜索工作並顯示結果。
在返回語句之前添加'dd($ launchsites);''。並告訴結果 –
你能看錯誤日誌嗎? –
@SimonSchnell我從數據庫中獲取需要的數據。 '$ launchsitesatellite'也一樣。 –