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.


  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


  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 {
  7. public function index(){
  8. return view('autocomplete.index');
  9. }
  10.     public function autoComplete(Request $request) {
  11. $query = $request->get('term','');
  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. }
  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="" rel="stylesheet">
  2. <script src=""></script>
  3. <script src=""></script>

Phone: (+91) 8800417876
Noida, 201301
okey metin2 pvp serverler metin2 pvp
ataşehir escort maltepe escort bostancı escort kadıköy escort maltepe escort ataşehir escort tuzla escort pendik escort ümraniye escort kartal escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort
hatay escort nevsehir escort nigde escort kirikkale escort batman escort osmaniye escort kirklareli escort tokat escort rize escort adiyaman escort gumushane escort batman escort
cialis satış cialis fiyat cialis 20 mg cialis 100 mg cialis 5 mg fiyat

viagra fiyatı

viagra 100 mg fiyat

viagra sipariş

orjinal viagra

viagra satın al

izmir escort
kocaeli escort izmit escort bayan gebze escort izmit sınırsız escort escort bursa tuzla escort aydınlı escort orhanlı escort bursa escort escort bayan istanbul escort istanbul sınırsız escort istanbul bayan escort izmit escort izmit escort kocaeli escort sisli escort mecidiyekoy escort bursa escort bayan bursa görükle escort maltepe escort goztepe escort yakacik-yeni-escort bursa sınırsız escort sisli escort bayan taksim escort bursa escort porno izle sikiş izle atasehir escort antalya escort
ankara escort marmaris escort bursa escort mersin escort antalya escort izmir escort samsun escort bodrum escort çeşme escort kuşadası escort adana escort eskişehir escort gaziantep escort esenyurt escort pendik escort tuzla escort fatih escort ümraniye escort mecidiyeköy escort kartal escort bahçelievler escort bahçeşehir escort ataşehir escort bakırköy escort nişantaşı escort avcılar escort büyükçekmece escort kurtkoy escort konya escort beşiktaş escort taksim escort beylikdüzü escort malatya escort sakarya escort şişli escort maltepe escort kadıköy escort ataköy escort bostancı escort şirinevler escort bağcılar escort üsküdar escort etiler escort başaksehir escort ortaköy escort kağıthane escort sultangazi escort halkalı escort çekmeköy escort şaşkınbakkal escort mamak escort sincan escort keçiören escort çankaya escort beypazarı escort akyurt escort pursaklar escort kahramankazan escort göztepe escort alsancak escort bornova escort buca escort karabaglar escort karşıyaka escort konak escort narlıdere escort izmit escort başiskele escort kandira escort körfez escort karamürsel escort dilovası escort gölcük escort gebze escort kocaeli escort kayseri escort elazığ escort denizli escort diyarbakir escort adana escort ankara escort izmir escort antalya escort gaziantep escort kayseri escort konya escort mersin escort denizli escort kocaeli escort diyarbakır escort