Manual Laravel Autocomplete search from Database
In this post we will give you information about Manual Laravel Autocomplete search from Database. Hear we will give you detail about Manual Laravel Autocomplete search from DatabaseAnd how to use it also give you demo for it if it is necessary.
Manual Laravel Autocomplete search from Database
Sometime we need autocomplete searchable text box which is reflect from database and we use third party plugins.Now I am going to tell you how to create manually autocomplete search text box in Laravel 5.2.
There should be installed Laravel 5.2 in your system if Laravel is not installed then install Laravel with following command.
composer create-project --prefer-dist laravel/laravel blog
Create a Products Table and Model
Follow below url till product table creation process or you can create ‘Products’ table in your database and relatively create model in following path app/Product.php
with name ‘Product’.
Laravel 5.2 CRUD (Create Read Update Delete) Example from Scratch
Insert some test data in your product table or you can use Laravel seed classes to seeding your database with test data.If you want to write seeders then run this command that generate seed file in database/seeds
directory.
php artisan make:seeder UsersTableSeeder
Now let’s start to creating autocomplete search text box.
Add Route and Controller
Add these two routes in your routes.php
file.
app/Http/routes.php
- Route::get('autocomplete',array('as'=>'autocomplete','uses'=>'AutoCompleteController@index'));
- Route::get('searchajax',array('as'=>'searchajax','uses'=>'AutoCompleteController@autoComplete'));
Route::get('autocomplete',array('as'=>'autocomplete','uses'=>'AutoCompleteController@index'));Route::get('searchajax',array('as'=>'searchajax','uses'=>'AutoCompleteController@autoComplete'));
Now we will create AutoCompleteController.php
in following path app/Http/Controllers
app/Http/Controllers/AutoCompleteController.php
- namespace AppHttpControllers;
- use IlluminateHttpRequest;
- use AppHttpControllersController;
- use AppProduct;
- class AutoCompleteController extends Controller {
- public functionindex(){
- returnview('autocomplete.index');
- }
- public functionautoComplete(Request $request){
- $query=$request->get('term','');
- $products=Product::where('name','LIKE','%'.$query.'%')->get();
- $data=array();
- foreach($productsas$product){
- $data[]=array('value'=>$product->name,'id'=>$product->id);
- }
- if(count($data))
- return$data;
- else
- return['value'=>'No Result Found','id'=>''];
- }
- }
namespace AppHttpControllers;use IlluminateHttpRequest;use AppHttpControllersController;use AppProduct;class AutoCompleteController extends Controller { public function index(){ return view('autocomplete.index'); } public function autoComplete(Request $request) { $query = $request->get('term',''); $products=Product::where('name','LIKE','%'.$query.'%')->get(); $data=array(); foreach ($products as $product) { $data[]=array('value'=>$product->name,'id'=>$product->id); } if(count($data)) return $data; else return ['value'=>'No Result Found','id'=>'']; } }
Create Blade File
Now we will create index.blade.php
file in following resources/views/autocomplete/ directory.
- @extends('layouts.default')
- @section('content')
- <divclass="row">
- <divclass="col-xs-12 col-sm-12 col-md-12">
- <divclass="form-group">
- {!! Form::text('search_text', null, array('placeholder' => 'Search Text','class' => 'form-control','id'=>'search_text')) !!}
- </div>
- </div>
- </div>
- <script>
- $(document).ready(function() {
- src = "{{ route('searchajax') }}";
- $("#search_text").autocomplete({
- source: function(request, response) {
- $.ajax({
- url: src,
- dataType: "json",
- data: {
- term : request.term
- },
- success: function(data) {
- response(data);
- }
- });
- },
- minLength: 3,
- });
- });
- </script>
- @endsection
@extends('layouts.default') @section('content') <div > <div > <div > {!! Form::text('search_text', null, array('placeholder' => 'Search Text','class' => 'form-control','id'=>'search_text')) !!} </div> </div> </div> <script> $(document).ready(function() { src = "{{ route('searchajax') }}"; $("#search_text").autocomplete({ source: function(request, response) { $.ajax({ url: src, dataType: "json", data: { term : request.term }, success: function(data) { response(data); } }); }, minLength: 3, });});</script>@endsection
Don’t forget to add Js and Css file in your master/default layout.
- <link href="//css/jquery.ui.autocomplete.css" rel="stylesheet">
- <script src="//js/jquery.js"></script>
- <script src="//js/jquery-ui.min.js"></script>
<link href="//css/jquery.ui.autocomplete.css" rel="stylesheet"><script src="//js/jquery.js"></script><script src="//js/jquery-ui.min.js"></script>
Hope this code and post will helped you for implement Manual Laravel Autocomplete search from Database. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs