Manual Laravel Autocomplete search from Database

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

  1. Route::get('autocomplete',array('as'=>'autocomplete','uses'=>'AutoCompleteController@index'));
  2. 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

  1. namespace AppHttpControllers;
  2. use IlluminateHttpRequest;
  3. use AppHttpControllersController;
  4. use AppProduct;
  5. class AutoCompleteController extends Controller {
  6.     
  7. public functionindex(){
  8. returnview('autocomplete.index');
  9. }
  10.     public functionautoComplete(Request $request){
  11. $query=$request->get('term','');
  12.         
  13. $products=Product::where('name','LIKE','%'.$query.'%')->get();
  14. $data=array();
  15. foreach($productsas$product){
  16. $data[]=array('value'=>$product->name,'id'=>$product->id);
  17. }
  18. if(count($data))
  19. return$data;
  20. else
  21. return['value'=>'No Result Found','id'=>''];
  22. }
  23.     
  24. }
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.

  1. @extends('layouts.default')
  2. @section('content')
  3. <divclass="row">
  4. <divclass="col-xs-12 col-sm-12 col-md-12">
  5. <divclass="form-group">
  6. {!! Form::text('search_text', null, array('placeholder' => 'Search Text','class' => 'form-control','id'=>'search_text')) !!}
  7. </div>
  8. </div>
  9. </div>
  10. <script>
  11.   $(document).ready(function() {
  12. src = "{{ route('searchajax') }}";
  13. $("#search_text").autocomplete({
  14. source: function(request, response) {
  15. $.ajax({
  16. url: src,
  17. dataType: "json",
  18. data: {
  19. term : request.term
  20. },
  21. success: function(data) {
  22. response(data);
  23. }
  24. });
  25. },
  26. minLength: 3,
  27. });
  28. });
  29. </script>
  30. @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.

  1. <link href="//css/jquery.ui.autocomplete.css" rel="stylesheet">
  2. <script src="//js/jquery.js"></script>
  3. <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>
Click here to see demo how to create manual autocomplete search textbox from database in Laravel 5.2
Show Demo

Label :

PHP

OOP

Object Oriented Programming

Laravel PHP Framework

jQuery

Web Development

JavaScript

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

For More Info See :: laravel And github

Leave a Comment

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

  +  12  =  20

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US