Spread the love

In this article we will learn to use Laravel Dynamic Autocomplete Using Typehead JS. Typehead JS is useful when we want live search of bulk data. Autocomplete search is mostly work of javascript and when we want to fetch live data from database then we require the intervention of laravel to provide the data in JSON response.

Typehead JS library is completely build for achieve the autocomplete suggestion functionality in the application with robust features of it .

In this example we will create a simple text box to search on type and give a list suggestion based on typed term. then in laravel we will create an API to response the result of search.

This example will work with any version of laravel like laravel 5, laravel 6, laravel 7, laravel 8 and laravel 9 too.

Let start the simple tutorial of Laravel Dynamic Autocomplete Using Typehead JS step by step

Step 1: Create a laravel project

First step is to create the Laravel project using composer command or you can also read the How to install laravel 8 ? and Laravel artisan command to generate controllers, Model, Components and Migrations

composer create-project laravel/laravel crud

Step 2: Configure database

Next step is to configure the database for our project, table and data. So open .env or if file not exist then rename .env.example file to .env file in root folder and change below details

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=

Step 3: Create Model and Migration

If you have table and large data already then you can skip this step but if you are beginner and trying to implement autosuggestion then in this step we are creating a model and migration using artisan command

php artisan make:model User -m

Step 4 : Create a controller

Next, Create a controller and two methods one for view and another for search the suggestion as follow

php artisan make:controller UserController

Now, add the controller logic

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{
    function index(){
        return view("users.search");
    }
    function search(Request $request){

        return  \App\Models\User::where("email","like","%{$request->term}%")
        ->select("id","email","name")
        ->limit(10)
        ->get();
    }
}

Here we created two methods index for show the view and search method for response the related users list according to keyword.

Step 5 : Create the view

Now, create a view file named as resources/views/users/search.blade.php and put the follow code

<!DOCTYPE html>
<html>

<head>
  <title>Laravel Dynamic Autocomplete Using Typehead JS - readerstacks.com</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" />

</head>

<body>
  <div class="container">

    <div class="panel panel-primary">
      <div class="panel-heading">
        <h2>Laravel Dynamic Autocomplete Using Typehead JS -readerstacks.com</h2>
      </div>
      <div class="panel-body">

        <div class="col-md-4">
          <label>Title</label>
          <input type="text" placeholder="Users" name="users" id='users' class="form-control">
        </div>

      </div>
    </div>
  </div>
</body>

</html>

<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>
<script type="text/javascript">
  var route = "{{url('users/search')}}";
  $('#users').typeahead({
    source: function(query, process) {
      return $.get(route, {
        query: query
      }, function(data) {
        return process(data);
      });
    }
  });
</script>

Now, here we imported the jQuery and jQuery UI js and CSS to implement the suggestion list autocomplete

Step 6 : Create Route

Last and final step is to create the route for our implementation

<?php

use Illuminate\Support\Facades\Route;

Route::get('/users',"\App\Http\Controllers\UserController@index");
Route::get('/users/search',"\App\Http\Controllers\UserController@search");

Live Screenshot :

Laravel Dynamic Autocomplete Using Typehead JS -readerstacks.com
Laravel Dynamic Autocomplete Using Typehead JS -readerstacks.com

Leave a Reply