Hello im learning Laravel and trying to make an autocompletion of search with sugestion from a database. I see a lot of people doing it with JQuery so that is what i've tried. But when i run the site, i only get one letter of suggestion, and sometimes no suggestion at all. I suspekt my routing is off so i tried dd some information in the query function, but that doesnt show up either,
MY BLADE
<!DOCTYPE html>
<html>
<head>
<title>Laravel 8 Autocomplete Search using Bootstrap Typeahead JS -ItSolutionStuff.com</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-3-typeahead/4.0.1/bootstrap3-typeahead.min.js"></script>
</head>
<body>
<div class="container">
<h1>Laravel 8 Autocomplete Search using Bootstrap Typeahead JS - ItSolutionStuff.com</h1>
<input class="typeahead form-control" type="text">
</div>
<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>
</body>
</html>
MY CONTROLLER
class TypeaheadController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('welcome');
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function autocomplete(Request $request)
{
dd('h');
$data = Edit::select("Adresse")
->where("Adresse","LIKE","%{$request->query}%")
->get();
return response()->json($data);
}
}
MY Routing
Route::get('search', [TypeaheadController::class, 'index'])->name('search');
Route::get('autocomplete', [TypeaheadController::class, 'autocomplete'])->name('autocomplete');
If you guys have any other suggestion on how i can do it. feel free to suggest
$request->query is a reserved function on the $request object. in order to pull the '?query=xyz' from the url in the request you would have to use the $request->query('query') function to return xyz.