How To Create Autocomplete Search In Laravel 9

In this article, we will see how to create autocomplete search in laravel 9. In this example, we will use ajax autocomplete textbox and get records from the database in laravel 9. Also, we will use the typehead javascript library to search records from the database.

The jQuery Typeahead plugin provides autocomplete preview on search inputs similar to google search with built-in options and deep customization.

So, let's see laravel 9 autocomplete search from the database, and autocomplete search using jquery in laravel 9.

Step 1: Add Route

Here, I have added the routes method in the route file for this autocomplete search example in laravel 9.


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;

Route::get('/', function () {
    return view('welcome');

Route::get('search', [UserController::class, 'search']);
Route::get('autocomplete', [UserController::class, 'autocomplete'])->name('autocomplete');


Step 2: Create Controller

Now, we have added the below code to the controller.


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use DB;

class UserController extends Controller
	public function search()
        return view('user');
    public function autocomplete(Request $request)
        $data = User::select("name")
        return response()->json($data);



Step 3: Create Blade File

In this step, we will create the user.blade.php file for the output view.

<!DOCTYPE html>
    <title>How To Create Autocomplete Search In Laravel 9 - Techsolutionstuff</title>
    <link rel="stylesheet" href="">
    <script src=""></script>
    <script src=""></script>
<div class="container">
    <h2 style="margin: 30px 0px; text-align: center;">How To Create Autocomplete Search In Laravel 9 - Techsolutionstuff</h2>   
    <input class="search form-control" type="text" placeholder="Search here...">
<script type="text/javascript">
    var path = "{{ route('autocomplete') }}";
        source:  function (str, process) 
          return $.get(path, { str: str }, function (data) {
                return process(data);


You might also like:


Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.