Laravel 5 Autocomplete using Bootstrap Typeahead JS Example with Demo

By Hardik Savani November 5, 2023 Category : Laravel Bootstrap Typeahead JS

Autocomplete is must if you are dealing with big data table, like you have products table and thousands of records so it's not possible to give drop-down box, but it is better if we use Autocomplete instead of select box.

In this example i use Bootstrap Typeahead JS plugin for auto-complete, Typeahead.js plugin provide good layout using bootstrap so it pretty good. You can implement autocomlete in your laravel application just following few step. After this example finish you will find out layout as bellow:


Step 1: Create items table and module

In First step we have to create migration for items table using Laravel 5 php artisan command, so first fire bellow command:

php artisan make:migration create_items_table

After this command you will find one file in following path database/migrations and you have to put bellow code in your migration file for create items table.

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;

class CreateItemsTable extends Migration


public function up()


Schema::create('items', function (Blueprint $table) {







public function down()





After craete "items" table you should craete Item model for items, so first create file in this path app/Item.php and put bellow content in item.php file:


namespace App;

use Illuminate\Database\Eloquent\Model;

use DB;

class Item extends Model


public $fillable = ['title','description'];


Step 2: Add Route

We need to add two route one for search view call and another for autocomplete so add two route in your routes.php file:




Step 3: Create controller

Ok, now we should create new controller as SearchController in this path app/Http/Controllers/SearchController.php. this controller will manage search page and autocoplete json, so put bellow content in controller file:


namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Controllers\Controller;

use App\Item;

class SearchController extends Controller



* Display a listing of the resource.


* @return \Illuminate\Http\Response


public function search()


return view('search');



* Show the form for creating a new resource.


* @return \Illuminate\Http\Response


public function autocomplete(Request $request)


$data = Item::select("title as name")->where("title","LIKE","%{$request->input('query')}%")->get();

return response()->json($data);



Step 4: Create View

In this step we have create search.blade.php file in resources directory.




<link href="" rel="stylesheet">

<script src=""></script>

<script src=""></script>

<div class="container">

<h1>Laravel 5 Autocomplete using Bootstrap Typeahead JS</h1>

<input class="typeahead form-control" style="margin:0px auto;width:300px;" type="text">


<script type="text/javascript">

var path = "{{ route('autocomplete') }}";


source: function (query, process) {

return $.get(path, { query: query }, function (data) {

return process(data);






Watch Online Video: