In this tutorial you will learn about the Laravel 7/6 Autocomplete Search using Jquery UI and its application with practical example.
In this Laravel 7/6 Autocomplete Search using Jquery UI tutorial I will show you how to create autocomplete input or search box using jquery ui in laravel. In this tutorial you will learn to create autocomplete input box using jquery ui in laravel.
In this example we will be using autocomplete jquery ui plugin. The jquery autocomplete plugin is used to create dynamic autocomplete input with several options. In this example you will learn how to implement jquery autocomplete in laravel.
Laravel 7/6 Autocomplete Search using Jquery UI
In this article, I will show you to create a dynamic database driven ajax jquery autocomplete in Laravel. You will also learn to create a dynamic search dropdown autocomplete which will fetch options from database table using jquery autocomplete.
- Install Laravel App
- Setup Database
- Generate Fake Records
- Make Routes
- Create Controller & Methods
- Create Blade View
- Conclusion
1: Install Laravel App
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 |
2: 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 |
3: Generate Fake Records
In this step we will add some dummy data into database.
1 |
php artisan migrate |
Use following command to add 100 fake records in database :
1 2 3 4 5 |
php artisan migrate php artisan tinker factory(App\User::class, 100)->create(); |
4: 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'); |
5: Create Controller
Now, lets create a controller named AutoCompleteController using command given below –
1 |
php artisan make:controller AutoCompleteController |
Now, 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); } } |
6: Create Blade view
In this step we will create a blade file. Go to app/resources/views and create one file name search.blade.php .
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
<!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>Auto Complete Search Using Jquery UI</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <style> .container{ padding: 10%; text-align: center; } </style> </head> <body> <div class="container"> <div class="row"> <div class="col-12"><h2>laravel 6 Auto Complete Search Using Jquery UI</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> $(document).ready(function() { $( "#search" ).autocomplete({ source: function(request, response) { $.ajax({ url: "{{url('autocomplete')}}", data: { term : request.term }, dataType: "json", success: function(data){ var resp = $.map(data,function(obj){ //console.log(obj.city_name); return obj.name; }); response(resp); } }); }, minLength: 1 }); }); </script> </body> </html> |
7: 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 |