Laravel 8 Autocomplete Search from Database

Websolutionstuff | Mar-01-2021 | Categories : Laravel PHP

In this article, we will see the laravel 8 autocomplete search from the database. Using ajax autocomplete textbox in laravel 8 we will get records from the database and get them in laravel. when you have more data in your table at a time you can't give a dropdown box because it will take more time to fetch data from the database. So, here we will add autocomplete search using ajax.

So, let's see ajax autocomplete textbox in laravel 7 and laravel 8, dynamic autocomplete in laravel, and autocomplete search in laravel 8 using typeahead.js

Step 1: Add Route

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


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 already created UserController for this example. So, you can create a controller as per your requirement and add 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 a user.blade.php file for the output view.

<!DOCTYPE html>
    <title>Laravel 8 Autocomplete Search from Database -</title>
    <link rel="stylesheet" href="">
    <script src=""></script>
    <script src=""></script>
<div class="container">
    <h2 style="margin: 30px 0px; text-align: center;">Laravel 8 Autocomplete Search from Database -</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:

Recommended Post
Featured Post
Laravel 9 QR Code Generator Example
Laravel 9 QR Code Generator Ex...

In this article, we will see laravel 9 QR code generator example. As per the current trend, many websites and appli...

Read More


What Is Digital Marketing?
What Is Digital Marketing?

Digital marketing is the component of marketing that utilizes internet and online bas...

Read More


Laravel 10 Apexcharts Bar Chart Example
Laravel 10 Apexcharts Bar Char...

In this article, we will see the laravel 10 apexcharts bar chart example. Here, we will learn about how to create a bar...

Read More


How To Send Email using Node.js
How To Send Email using Node.j...

Hello Friends, In this tutorial we will learn how to send email using node.js app. In this tutorial we will see send...

Read More