In this tutorial you will learn about the Laravel 7/6 Autocomplete using Typeahead Js and its application with practical example.
In this Laravel 7/6 Autocomplete using Typeahead Js, I will show you how to create a dynamic database driven ajax jquery autocomplete using typeahead js in Laravel. In this tutorial we will create a dynamic search dropdown autocomplete which will fetch options from database table using bootstrap typeahead js.
The Typeahead JS is a jquery plugin, it is used to create dynamic autocomplete input with several options. In this example you will learn how to implement typeahead autocomplete in laravel.
Laravel 7/6 Autocomplete using Typeahead Js
In this step by step tutorial I will demonstrate how to create a dynamic database driven ajax jquery autocomplete using typeahead js in Laravel. Please follow the instruction given below:
- Install Laravel
- Setup Database
- Generate Fake Records
- Make Routes
- Create Controller & Methods
- Create Blade View
- Conclusion
Install Laravel
First of all we need to create a fresh laravel project, download and install Laravel using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Setup Database
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=here your database name here DB_USERNAME=here database username here DB_PASSWORD=here database password here |
Generate Fake Records
In this step we will insert dummy data into database
1 |
php artisan migrate |
Now we will add fake records in our database. use the below command and add 100 fake records in database :
1 2 3 |
php artisan tinker factory(App\User::class, 100)->create(); |
Make Routes
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 |
Route::get('search', 'AutoCompleteController@index'); Route::get('autocomplete', 'AutoCompleteController@search'); |
Create Controller
Now, lets create a controller named AutoCompleteController using command given below –
1 |
php artisan make:controller AutoCompleteController |
After successfully create controller go to app/controllers/AutoCompleteController.php and put the below code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\User; class AutoCompleteController extends Controller { public function index() { return view('search'); } public function search(Request $request) { $search = $request->get('term'); $result = User::where('name', 'LIKE', '%'. $search. '%')->get(); return response()->json($result); } } |
Create Blade view
In this step we need to create blade view file. Go to app/resources/views and create one file name search.blade.php . After create blade file put the below html code here with jquery ui and css library file :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <meta name="csrf-token" content="{{ csrf_token() }}"> <title>Autocomplete Search Using Typeahead JS</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" /> <script src="//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> <style> .container{ padding: 10%; text-align: center; } </style> </head> <body> <div class="container"> <div class="row"> <div class="col-12"><h2>Laravel AutoComplete Search Using Typeahead JS</h2></div> <div class="col-12"> <div id="custom-search-input"> <div class="input-group"> <input id="search" name="search" type="text" class="form-control" placeholder="Search" /> </div> </div> </div> </div> </div> <script type="text/javascript"> var route = "{{ url('autocomplete') }}"; $('#search').typeahead({ source: function (term, process) { return $.get(route, { term: term }, function (data) { return process(data); }); } }); </script> </body> </html> |
Start Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://localhost:8000/search |