This is the basic code. I cannot seem to get it to work in Laravel 5:
routes.php
Route::get('h2h', 'atp_players\H2hController@getIndex');
Route::get('h2h_getdata', 'atp_players\H2hController@getData');
H2hController.php
namespace Atpstats\Http\Controllers\atp_players;
use Atpstats\Http\Controllers\Controller;
use Response;
use Request;
class H2hController extends Controller{
public function getIndex() {
return view('atp_players.h2h');
}
public function getData() {
$term = Request::input('auto', 'r');
$results = \DB::table('atp_players')->select('firstname')->get();
$data = array();
foreach($results as $result) {
if(strpos($result,$term) !== false) {
$data[] = ['value' => $result->firstname];
}
}
return Response::json($data);
}
}
View: h2h.blade.php
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Autocomplete test</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
</head>
<body>
<div class="container">
<div class="ui-widget">
<label for="">Find a player</label>
<input type="text" class="form-control input-sm" name="auto" id="auto" autocomplete="on">
</div>
<div class="form-group">
<label for="">Response</label>
<input type="text" class="form-control input-sm" name="response" id="response" disabled>
</div>
<script>
$('#auto').autocomplete({
type: "get",
source: 'h2h_getdata',
dataType: "json",
minLength: 1,
select:function(e,ui){
$('#response').val(ui.item.value);
}
});
</script>
</body>
</html>
It doesn't work. It's doing nothing event when you write something to the input text case. But if I comment some lines in Controller, the data (firstname) of the DB appear but autocomplete doesn't work.
And that's the modified Controller function:
public function getData() {
//$term = Request::input('auto', 'r');
$results = \DB::table('atp_players')->select('firstname')->get();
$data = array();
foreach($results as $result) {
//if(strpos($result,$term) !== false) {
$data[] = ['value' => $result->firstname];
//}
}
return Response::json($data);
}