Manual Laravel Autocomplete search from Database

Manual Laravel Autocomplete search from Database

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'));

Now we will create AutoCompleteController.php in following path app/Http/Controllers

app/Http/Controllers/AutoCompleteController.php

  1. namespace App\Http\Controllers;
  2. use Illuminate\Http\Request;
  3. use App\Http\Controllers\Controller;
  4. use App\Product;
  5. class AutoCompleteController extends Controller {
  6.     
  7. public function index(){
  8. return view('autocomplete.index');
  9. }
  10.     public function autoComplete(Request $request) {
  11. $query = $request->get('term','');
  12.         
  13. $products=Product::where('name','LIKE','%'.$query.'%')->get();
  14. $data=array();
  15. foreach ($products as $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. }
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. <div class="row">
  4. <div class="col-xs-12 col-sm-12 col-md-12">
  5. <div class="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
Don't forget to add Js and Css file in your master/default layout.

  1. <link href="http://demo.expertphp.in/css/jquery.ui.autocomplete.css" rel="stylesheet">
  2. <script src="http://demo.expertphp.in/js/jquery.js"></script>
  3. <script src="http://demo.expertphp.in/js/jquery-ui.min.js"></script>

Phone: (+91) 8800417876
Noida, 201301
sakarya escort akyazı escort arifiye escort erenler escort eve gelen escort ferizli escort geyve escort hendek escort otele gelen escort sapanca escort söğütlü escort taraklı escort
sakarya escort akyazı escort arifiye escort erenler escort eve gelen escort ferizli escort geyve escort hendek escort karapürçek escort karasu escort kaynarca escort kocaali escort otele gelen escort pamukova escort sapanca escort söğütlü escort taraklı escort
sakarya escort akyazı escort arifiye escort erenler escort eve gelen escort ferizli escort geyve escort hendek escort karapürçek escort karasu escort kaynarca escort kocaali escort otele gelen escort pamukova escort sapanca escort söğütlü escort taraklı escort
sakarya escort akyazı escort arifiye escort erenler escort eve gelen escort ferizli escort geyve escort hendek escort karapürçek escort karasu escort kaynarca escort kocaali escort otele gelen escort pamukova escort sapanca escort söğütlü escort taraklı escort
sakarya escort serdivan escort adapazarı escort sakarya escort bayan sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sapanca escort sakarya escort escort bayan