Laravel 5 Autocomplete using Bootstrap Typeahead JS Example with Demo

Laravel 5 Autocomplete using Bootstrap Typeahead JS Example with Demo

In this post we will give you information about Laravel 5 Autocomplete using Bootstrap Typeahead JS Example with Demo. Hear we will give you detail about Laravel 5 Autocomplete using Bootstrap Typeahead JS Example with DemoAnd how to use it also give you demo for it if it is necessary.

Autocomplete is must if you are dealing with big data table, like you have products table and thousands of records so it’s not possible to give drop-down box, but it is better if we use Autocomplete instead of select box.

In this example i use Bootstrap Typeahead JS plugin for auto-complete, Typeahead.js plugin provide good layout using bootstrap so it pretty good. You can implement autocomlete in your laravel application just following few step. After this example finish you will find out layout as bellow:

Preview:

Step 1: Create items table and module

In First step we have to create migration for items table using Laravel 5 php artisan command, so first fire bellow command:

php artisan make:migration create_items_table

After this command you will find one file in following path database/migrations and you have to put bellow code in your migration file for create items table.

use IlluminateDatabaseSchemaBlueprint;

use IlluminateDatabaseMigrationsMigration;

class CreateItemsTable extends Migration

{

public function up()

{

Schema::create('items', function (Blueprint $table) {

$table->increments('id');

$table->string('title');

$table->text('description');

$table->timestamps();

});

}

public function down()

{

Schema::drop("items");

}

}

After craete “items” table you should craete Item model for items, so first create file in this path app/Item.php and put bellow content in item.php file:

app/Item.php

namespace App;

use IlluminateDatabaseEloquentModel;

use DB;

class Item extends Model

{

public $fillable = ['title','description'];

}

Step 2: Add Route

We need to add two route one for search view call and another for autocomplete so add two route in your routes.php file:

app/Http/routes.php

Route::get('search',array('as'=>'search','uses'=>'SearchController@search'));

Route::get('autocomplete',array('as'=>'autocomplete','uses'=>'SearchController@autocomplete'));


Step 3: Create controller

Ok, now we should create new controller as SearchController in this path app/Http/Controllers/SearchController.php. this controller will manage search page and autocoplete json, so put bellow content in controller file:

app/Http/Controllers/SearchController.php

namespace AppHttpControllers;


use IlluminateHttpRequest;

use AppHttpControllersController;

use AppItem;


class SearchController extends Controller

{


/**

* Display a listing of the resource.

*

* @return IlluminateHttpResponse

*/

public function search()

{

return view('search');

}


/**

* Show the form for creating a new resource.

*

* @return IlluminateHttpResponse

*/

public function autocomplete(Request $request)

{

$data = Item::select("title as name")->where("title","LIKE","%{$request->input('query')}%")->get();

return response()->json($data);

}

}

Step 4: Create View

In this step we have create search.blade.php file in resources directory.

resources/views/search.blade.php

@extends('layouts.app')


@section('content')


<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-3-typeahead/4.0.1/bootstrap3-typeahead.min.js"></script>


<div >


<h1>Laravel 5 Autocomplete using Bootstrap Typeahead JS</h1>

<input style="margin:0px auto;width:300px;" type="text">


</div>


<script type="text/javascript">

var path = "{{ route('autocomplete') }}";

$('input.typeahead').typeahead({

source: function (query, process) {

return $.get(path, { query: query }, function (data) {

return process(data);

});

}

});

</script>


@endsection

Hope this code and post will helped you for implement Laravel 5 Autocomplete using Bootstrap Typeahead JS Example with Demo. 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 *

9  +  1  =  

We accept paid advance and paid guest Posting on our Site : Contact US