In this tutorial you will learn about the Laravel 9 Autocomplete Search with jQuery UI and its application with practical example.
In this Laravel 9 Autocomplete Search using Jquery UI tutorial I will show you how to create autocomplete input or search box using jquery ui in laravel 9. In this tutorial you will learn to create autocomplete input box using jquery ui in laravel 9. 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 9.
Laravel 9 Autocomplete Search with jQuery UI
In this article, I will show you to create a dynamic database driven ajax jquery autocomplete in Laravel 9. You will also learn to create a dynamic search dropdown autocomplete which will fetch options from database table using jquery autocomplete.
- Install Laravel 9
- Database Configuration Database
- Make Model and Migration
- Make Routes
- Create Controller
- Create Blade View
- Implement jQuery Ajax Code
- Run Development Server
Install Laravel 9
First of all we need to create a fresh laravel project, download and install Laravel 9 using the below command
1 |
composer create-project --prefer-dist laravel/laravel lara9blog |
Database Configuration 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=lara9blog DB_USERNAME=root DB_PASSWORD= |
Make Model and Migration
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan make:model Product -m |
Once above command is executed there will be a migration file created inside database/migrations/ directory, just open migration file and update the function up() method as following:
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 |
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateProductsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('products', function (Blueprint $table) { $table->id(); $table->string('name'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('products'); } } |
Next, go to app/Models/Product.php file and add the $fillable property in the Product model as following.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Product extends Model { use HasFactory; protected $fillable = [ 'name' ]; } |
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 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\AutoCompleteController; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('search', [AutoCompleteController::class, 'index'])->name('search'); Route::get('autocomplete', [AutoCompleteController::class, 'autocomplete'])->name('autocomplete'); |
Create Controller
Now, lets create a controller named AutoCompleteController using command given below –
1 |
php artisan make:controller AutoCompleteController |
Once the above command executed, it will create a controller file AutoCompleteController.php in app/Http/Controllers/ directory. Open the AutoCompleteController.php file and put the following code in it.
app/Http/Controllers/AutoCompleteController.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 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Product; class AutoCompleteController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { return view('autocomplete-search'); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function autocomplete(Request $request) { $res = Product::select("name") ->where("name","LIKE","%{$request->term}%") ->get(); return response()->json($res); } } |
Create Blade View
In this step we will create a blade file. Go to app/resources/views and create one file name autocomplete-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 |
<!DOCTYPE html> <html> <head> <title>Laravel 9 Autocomplete Textbox From Database with jQuery ui</title> <meta name="csrf-token" content="{{ csrf_token() }}"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.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> <script type="text/javascript"> var siteUrl = "{{url('/')}}"; </script> </head> <body> <div class="container mt-4"> <div class="card"> <div class="card-header text-center font-weight-bold"> <h2>Laravel 9 Autocomplete Textbox From Database using jQuery ui</h2> </div> <div class="card-body"> <form name="autocomplete-textbox" id="autocomplete-textbox" method="post" action="#"> @csrf <div class="form-group"> <label for="exampleInputEmail1">Search Product By Name</label> <input type="text" id="name" name="name" class="form-control"> </div> </form> </div> </div> </div> <script src="{{ asset('auto.js') }}"></script> </body> </html> |
Implement jQuery Ajax Code
Next, go to public directory and create one file name auto.js. Open auto.js file and add the following code in auto.js file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
$(document).ready(function() { $( "#name" ).autocomplete({ source: function(request, response) { $.ajax({ url: siteUrl + '/' +"autocomplete", data: { term : request.term }, dataType: "json", success: function(data){ var resp = $.map(data,function(obj){ return obj.name; }); response(resp); } }); }, minLength: 2 }); }); |
Run 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 |