Laravel 6 Autocomplete Search Using Typeahead Js Example

How to implement autocomplete search with Database in laravel 6 app with jquery typeahead js. In this laravel tutorial, We will share with you how to implement autocomplete search with database using jquery typeahead js example.

Today we will implement autocomplete search using jquery typeahead js simple and easy way. Just follow the few steps and implement autocomplete search in your laravel Application.

Jquery Typeahead js is bootstrap library that is work for autocomplete search in laravel . Using typeahead js you can display the search results and it also provides several options for customization.

AutoComplete Search Using Typehead Js

Contents

  • Install Laravel
  • Setup Database
  • Generate Fake Records
  • Make Routes
  • Create Controller & Methods
  • Create Blade View
  • Conclusion

Install Laravel

First of we need to download laravel fresh setup. Use the below command and download fresh new laravel setup :

composer create-project --prefer-dist laravel/laravel blog

Setup Database

After successfully install laravel Application, Go to your project .env file and set up database credential and move next step :

 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

Generate Fake Records

Before we add fake records in database first migrate the table in database using the below command :

php artisan migrate

We need to add fake records in our database. use the below command and add 100 fake records in database :

php artisan tinker 

factory(App\User::class, 100)->create();

Make Routes

Now we will create two routes one for show search input box and second for autocomplete search using jquery typeahead js autocomplete method :

 Route::get('search', '[email protected]');
Route::get('autocomplete', '[email protected]');

Create Controller

We need to create a controller name AutoCompleteController. Use the below command and create Controller :

php artisan make:controller AutoCompleteController

After successfully create controller go to app/controllers/AutoCompleteController.php and put the below code :

<?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);
           
    } 
}

Create Blade view

In this step we need to create blade view file. Go to app/resources/views and create one file name search.blade.php .

After create blade file put the below html code here with jquery ui and css library file :

<!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>Autocomplete Search Using Typeahead JS - Tutsmake.com</title>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" />
  <script src="http://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>
 <style>
    .container{
    padding: 10%;
    text-align: center;
   } 
 </style>
</head>
<body>

<div class="container">
    <div class="row">
        <div class="col-12"><h2>Laravel AutoComplete Search Using Typeahead JS</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 type="text/javascript">
    var route = "{{ url('autocomplete') }}";
    $('#search').typeahead({
        source:  function (term, process) {
        return $.get(route, { term: term }, function (data) {
                return process(data);
            });
        }
    });
</script>
  
</body>
</html>

We have put some script code in search.blade.php file. The script code will work to search a data from database and return data to our view file this ajax method full field over autocomplete search example.

Start Development Server

We need to start development server. Use the php artisan serve command and start your server :

 php artisan serve

If you want to run the project diffrent port so use this below command

php artisan serve --port=8080

Now we are ready to run our example so run bellow command to quick run.

 http://localhost:8000/search

Conclusion

In this tutorial , We have successfully implemented autocomplete search in our laravel application using typeahead js. our examples run quickly.

If you have any questions or thoughts to share, use the comment form below to reach us.

29 Comments

  1. 123123 said:

    sql injection incomming

    October 27, 2019
    Reply
  2. dirk said:

    that does not work with the actual release …

    January 25, 2020
    Reply
  3. domoopelo said:

    Cialis 5 Mg Costo Eraveden cialis buy online fradrady cialis nitric oxide

    December 13, 2020
    Reply
  4. Niamh said:

    Great post. I’m facing a few of these issues as well..

    my web-site cheap flights

    January 31, 2021
    Reply
  5. Jack said:

    Very nice post. I just stumbled upon your weblog and wanted to say that I’ve really enjoyed browsing your blog posts.
    In any case I’ll be subscribing to your rss feed and I hope you write
    again very soon!

    Feel free to visit my webpage: cheap flights

    February 1, 2021
    Reply
  6. Isabell said:

    We are a group of volunteers and opening a new scheme in our community.

    Your web site provided us with valuable information to work on.
    You’ve done an impressive job and our entire community will be thankful to you.

    Also visit my site … cheap flights; tinyurl.com,

    February 2, 2021
    Reply
  7. Joni said:

    Please let me know if you’re looking for a article writer for your blog.
    You have some really great posts and I believe I would be a good
    asset. If you ever want to take some of the load off, I’d absolutely
    love to write some articles for your blog in exchange for a link back to mine.
    Please shoot me an email if interested. Cheers!

    Here is my page cheap flights

    February 2, 2021
    Reply
  8. Consuelo said:

    I must thank you for the efforts you have put in penning this site.
    I really hope to view the same high-grade content by you
    in the future as well. In truth, your creative writing abilities has inspired me to get
    my own, personal site now 😉

    Look into my web site – cheap flights (tinyurl.com)

    February 3, 2021
    Reply
  9. Jbnbbluch said:

    buy prescription drugs from canada canadadrugs health canada drug database

    February 9, 2021
    Reply
  10. Fqbbguany said:

    good payday loan sites dublin ohio payday loans 24+ advanced learning loan birth or adoption certificate form

    February 11, 2021
    Reply
  11. Albertha said:

    I’m extremely impressed along with your writing talents and also with the layout
    to your weblog. Is this a paid subject matter or did you customize it yourself?
    Anyway stay up the excellent quality writing, it is rare to
    look a nice weblog like this one these days..

    my web-site: cheap flights (tinyurl.com)

    February 12, 2021
    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *