onlinecode

Laravel Dynamic Autocomplete Search using Select2 JS Ajax – Part 2

Laravel Dynamic Autocomplete Search using Select2 JS Ajax – Part 2

In this post we will give you information about Laravel Dynamic Autocomplete Search using Select2 JS Ajax – Part 2. Hear we will give you detail about Laravel Dynamic Autocomplete Search using Select2 JS Ajax – Part 2And how to use it also give you demo for it if it is necessary.

After complete 4 steps in our Part 1, we have to proceed next step from Part 2. In this part we write code for how to manage controller method and how to give response them. We also write code for view blade layout files.

This part is hart of “Laravel 5 dynamic autocomplete search using select2 JS Ajax” article, In this part you will see the logic of database and blade template. So you have to follow this bellow remaining steps.

Step 5: Create Controller

In this point, now we should create new controller as Select2AutocompleteController in this path app/Http/Controllers/Select2AutocompleteController.php. this controller will manage layout and select2 ajax request, So run bellow command for generate new controller:

php artisan make:controller Select2AutocompleteController

Ok, now put bellow content in controller file:

app/Http/Controllers/Select2AutocompleteController.php

<?php


namespace AppHttpControllers;


use IlluminateHttpRequest;

use DB;


class Select2AutocompleteController extends Controller

{

/**

* Show the application layout.

*

* @return IlluminateHttpResponse

*/

public function layout()

{

return view('select2');

}


/**

* Show the application dataAjax.

*

* @return IlluminateHttpResponse

*/

public function dataAjax(Request $request)

{

$data = [];


if($request->has('q')){

$search = $request->q;

$data = DB::table("categories")

->select("id","name")

->where('name','LIKE',"%$search%")

->get();

}


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

}

}

Step 6: Create View

In Last step, let’s create one blade file that will render view of autocomplete task. In this file i used cdn of bootstrap, jquery and select2 plugin.

resources/views/select2.blade.php

<html lang="en">


<head>

<title>Laravel 5 - Dynamic autocomplete search using select2 JS Ajax</title>

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

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

<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />

<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>

</head>


<body>


<div >


<h2>Laravel 5 - Dynamic autocomplete search using select2 JS Ajax</h2>

<br/>

<select style="width:500px;" name="itemName"></select>


</div>


<script type="text/javascript">


$('.itemName').select2({

placeholder: 'Select an item',

ajax: {

url: '/select2-autocomplete-ajax',

dataType: 'json',

delay: 250,

processResults: function (data) {

return {

results: $.map(data, function (item) {

return {

text: item.name,

id: item.id

}

})

};

},

cache: true

}

});


</script>


</body>

</html>

Now we are ready to run our example so run bellow command ro quick run:

php artisan serve

Now you can open bellow URL on your browser:

Also see:Jquery email autocomplete example with demo using jquery.email-autocomplete.min.js

http://localhost:8000/select2-autocomplete

I hope it can help you…

Hope this code and post will helped you for implement Laravel Dynamic Autocomplete Search using Select2 JS Ajax – Part 2. 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

Exit mobile version