Laravel 8 Autocomplete Search from the Database Using Ajax and Jquery


Laravel 8 Autocomplete Search from Database Using Ajax and Jquery
Laravel 8 Autocomplete Search from Database Using Ajax and Jquery

In this tutorial, we will learn Laravel 8 autocomplete search from the database using ajax and Jquery.

The autocomplete search is very helpful because we can search the data from the database just by typing the information we want instead of reloading the page. Also, it is a very important part of a good UI of the application.

We are able to do the autocomplete search because of AJAX and Jquery. Without AJAX, we can not search the data on the fly i.e. without submitting any search button.

Required steps of Laravel 8 autocomplete search from database

Step 1:- Create a project in laravel 8

Please Read, How to create laravel project from scratch step by step

Step 2:- Set up the database.

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

Step 3:- Create a table in the database

CREATE TABLE `employee` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `age` varchar(20) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `department` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

Step 4:- Set up routes in the web.php file inside the routes folder as shown below

Route::get('/searchdata',[App\Http\Controllers\EmployeeController::class, 'index']);
Route::get('/empsearch',[App\Http\Controllers\EmployeeController::class, 'searchemp']);

We can see from the above routes that the EmployeeController has two methods

  • index()
  • searchemp()

The index() method is used for viewing the employee information from the database in the blade file.

On the other hand, the searchemp() method is used to search the required information from the database based on the user request and return the response data to the user.

Step 5:- Create a model using the artisan command in the command terminal as shown below

php artisan make:model Employee

The above command will create a model like app\Models\Employee.php

Employee.php:-

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Employee extends Model
{
    use HasFactory;
    protected $table = 'employee';
     protected $fillable = [
        'name','age','department',
     ];
}

Step 6:- Create a controller using the artisan command in the command terminal as shown below

php artisan make:controller EmployeeController

EmployeeController.php:-

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Employee;
use DB;
use Auth;

class EmployeeController extends Controller
{
    //
    public function index(){

    	$data = Employee::all();

    	return view('searchdata',compact('data'));
    }

    public function searchemp(Request $request){
    $searchvalue = $request->search;

    if($request->ajax()){
          $empinfo = DB::table('employee')
                ->where('name','LIKE','%'.$searchvalue.'%')
                ->get();
           
	if (count($empinfo)>0) {
	      
	        $output = '';
	        foreach ($empinfo as $key=>$value){
	           
	        $output .= '<tr>
			                  <td>'.($key+1).'</td>
			                  <td>'.($value->name).'</td>
			                  <td>'.$value->age.'</td>
			                  <td>'.$value->department.'</td>
			              </tr>';
	                }

	       }
	else {
	    $empinfo = Employee::all();
	             
	    $output = '';
	      
	       foreach ($empinfo as $key=>$value){
	        $output .= '<tr>
			                  <td>'.($key+1).'</td>
			                  <td>'.($value->name).'</td>
			                  <td>'.$value->age.'</td>
			                  <td>'.$value->department.'</td>
			              </tr>';
	                }

	            }
	            return $output;
        }
  }

}

Step 7:- Create a blade file inside the resources\views folder of the laravel project.

searchdata.blade.php:-

<html>
<head>
<meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<style>
.container{
	width:50%;
	height:30%;
	padding:20px;
}
</style>
</head>

<body>
	<div class="container">
	<br/><br/>
	<div class="row">
		<div class="col-sm-8">
			<div class="form-group">
			    <label for="name">Search Employee Data:</label>
			    <input type="search" class="form-control" onkeyup="fetchDataEmp()" id="find" placeholder="search here">
			    
			</div>
		</div>
				
	</div>
	<br/>
	<h4>Search Result:</h4>
	    <table class="table table-bordered">
		    <thead>
		      <tr>
		        <th>SL No</th>
		        <th>Name</th>
		        <th>Age</th>
		        <th>Department</th>
		      </tr>
		    </thead>
		    <tbody>
		    @foreach($data as $key=>$value)
		      <tr>
		        <td>{{$key+1}}</td>
		        <td>{{$value->name}}</td>
		        <td>{{$value->age}}</td>
		        <td>{{$value->department}}</td>
		      </tr>
		    @endforeach
		    </tbody>
	  	</table>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<script>
function fetchDataEmp(){
  var search = $('#find').val();
  //alert(search);
 $.ajax({
    url: 'empsearch',
    type: 'get',
    data: {'search':search},
    success:function(data){
        $('tbody').html(data);
    }
  });
}
</script>
</body>
</html>

Step 8:- Now, open the terminal and type the command as shown below

php artisan serve

The above command will generate the link as shown below

http://127.0.0.1:8000

Step 9:- Now, open the browser and hit the below URL

http://127.0.0.1:8000/searchdata
 

Conclusion:- I hope this tutorial will help you to understand the overview. If you have any doubt then please leave a comment below.


Leave a Comment

RATU311

RATU311

RATU311

KUPU178

THOR311

THOR311

THOR311

THOR311

KUPU178

RATU311

RATU311

RATU311

slot online

kupu178

thor311

thor311

kupu178

kupu178

RATU311

RATU311

KUPU178

THOR311

THOR311

THOR311

KUPU178

slot mahjong ways

hoki311

slot gacor

ceri188

ratu311

kupu178

Casino Baccarat Online

kupu178

kupu178

ceri188

ceri188

THOR311

RATU311

KUPU178

THOR311

RATU311

toto macau

KUPU178

slot gacor

HOKI311

sabung ayam online

RATU311

SLOT LUCKY NEKO

CERI188

Slot Gacor

KUPU178

RATU311

KUPU178

RATU311

Link Spaceman

CERI188

Slot Gacor Online

THOR311

Mahjong ways

KUPU178

THOR311

THOR311

Live Casino

HOKI311

CERI188

CERI188

KUPU178

THOR311

THOR311

THOR311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

THOR311

KUPU178

KUPU178

CERI188

HOKI311 SLOT

TOTO TOGEL TOKYO

CERI188

CERI188 SITE

CERI188

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

THOR311

Toto Togel

KUPU178

Slot Mahjong

CERI188

ARENA SABUNG AYAM

RATU311

CERI188

CERI188

Slot Sweet Bonanza 2500

KUPU178

SLOT ONLINE

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178 X SLOT777

CERI188

sabung ayam online

THOR311

judi bola online

THOR311

KUPU178 10 GAME SLOT TERBAIK

KUPU178

KUPU178 SLOT TERPERCAYA

KUPU178 TOGEL

TOTO 4D MACAU

KUPU178

TOGEL ONLINE

KUPU178

SITUS BOLA

CERI188

KUPU178

THOR311

RATU311

CERI188

THOR311

THOR311

CERI188

HOKI311

CERI188

KUPU178

KUPU178

KUPU178

KUPU178

RATU311

THOR311

CERI188

Slot Mahjong Ways

KUPU178

CERI188

Slot Online Resmi

KUPU178

KUPU178

RATU311 SLOT ONLINE

KUPU178

KUPU178 TOTO MACAU

KUPU178: Link Akses Resmi

KUPU178: Strategi Menang Di Game Live Casino

KUPU178

KUPU178: Minimal 200 Rupiah

Slot Gacor Online

KUPU178

Mix Parlay

CERI188

CERI188

THOR311

THOR311

THOR311

THOR311

THOR311

KUPU178

CERI188

Slot Gacor Online

KUPU178

Slot Thailand

CERI188

CERI188

THOR311 RTP

RATU311

RATU311

RATU311

Mahjong Ways 2

CERI188

CERI188

CERI188

KUPU178 TOTO SLOT

CERI188 TOTO Singapore

THOR311

KUPU178

KUPU178

KUPU178

RATU311

CERI188

CERI188

RATU311: Alternatif karnpuracollege

RATU311

RATU311

Thor311 Terbaru

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188

CERI188

CERI188: Masuk Akun Slot Super Cepat

THOR311 PRO

THOR311

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

THOR311

HOKI311

HOKI311

RATU311

KUPU178

HOKI311

KUPU178

RATU311

THOR311 TOTO4D

RATU311

RATU311

RATU311

RATU311

THOR311

CERI188

DAFTAR KUPU178

LINK DAFTAR CERI188

CERI188

CERI188

CERI188

RATU311

KUPU178

THOR311 DOMINO QQ

judi bola online

Mix Parlay

CERI188

CERI188

live casino online

RATU311

Mix Parlay

Slot PG Soft

CERI188

RATU311

RATU311

CERI188

THOR311

CERI188

THOR311

Game Thor311

HOKI311

HOKI311

Thor311 Alternatif

RATU311

HOKI311

THOR311 RESMI

THOR311 AKSES

HOKI311

THOR311

RATU311 NONTON BOLA

THOR311

Thor311 Live Casino

CERI188

akses VIP THOR311

THOR311

link daftar THOR311

THOR311

situs thor311

RATU311

HOKI311 Link QRIS EWALLET

RATU311

HOKI311 Link RTP Gacor

HOKI311 Link Apk Resmi

CERI188

KUPU178 Link Daftar

CERI188

CERI188

CERI188

CERI188

CERI188

HOKI311

RATU311 judi bola

KUPU178

HOKI311 Live Casino Online

situs judi bola

CERI188

link login CERI188

link login CERI188

CERI188

HOKI311

CERI188

THOR311

HOKI311

HOKI311

HOKI311

RATU311

THOR311

KUPU178

HOKI311

KUPU178

KUPU178

kupu178 link alternatif

kupu178 slot

kupu178 daftar

kupu178 login

link daftar kupu178

judi bola

HOKI311

RATU311

link alternatif RATU311

Link daftar RATU311

link login RATU311

RATU311

RATU311

KUPU178

KUPU178

KUPU178

RATU311

RATU311

THOR311

KUPU178

KUPU178

HOKI311

THOR311

KUPU178

THOR311

THOR311

slot thailand

judi bola

THOR311

KUPU178

THOR311

THOR311

slot gacor

slot gacor

yakuza303

slot gacor

RATU311

THOR311