how to autocomplete search use laravel | Laravel autocomplete search

Author Name : Md. Shahab uddin Category Name : Laravel

how to autocomplete search use laravel | Laravel autocomplete search

//its input field
<input type="text" name="employee_name" id="employee_name_auto_complete" class="form-control">

//its for link
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

//its for autocomplete by text box start


$("#employee_name_auto_complete").autocomplete({
source: function (request, response) {
var path = "{{ route('all-employee') }}";
$.ajax({
url: path,
dataType: "json",
data: {
term: request.term,
cityid: $("#dl_cityname").val()
},
success: function (data) {
// alert(data);
response(data);
}
});
},
search: function () {
$(this).addClass('working');
},
open: function () {
$(this).removeClass('working');
}
});


//its for autocomplete by text box close

 

//its for route


Route::get('/all-employee/{term?}{cityid?}', 'EmployeeController@all_employee')->name('all-employee');

 

// its for controller


public function all_employee(Request $request) {
$term = $request->input('term');
$city = $request->input('cityid');
// echo $term; die();
$allemployees = Employee::where('employee_id', 'LIKE', '%' . $term . '%')->where('restcityid', $city)->get();
// echo '<pre>'; print_r($allemployees);echo '</pre>'; die();
return view('Dashboard.employee.employee_autocomplete')->with('allemployees', $allemployees);
}

 

// its view page

 

<?php

//echo '<pre>';print_r($allemployees); echo '</pre>';die();
$a = array();
foreach ($allemployees as $single) {
if ($single->employee_id) {
$a[] = html_entity_decode($single->id . "->" . $single->name . "( " . $single->address . "," . $single->mobile_no . " )" . "->( " . $single->employee_id . " )");
} else {

}
}
//$b = array_unique($a);
$i = 0;
echo '["';
$c = array();
foreach ($a as $key => $brec) {
$c [] = $brec;
}
echo implode('","', $c);
echo '"]';
//ie();