Laravel Dynamic Autocomplete Search using Select2 JS Ajax - Part 1
Today, I am going to share with you how to make dynamic autocomplete select dropdown from database using select2.js plugin in our laravel 5, laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 application.
Why we have to use autocomplete for our project ?, Sometimes we have more or thousands of records on our tables like users, products, tags etc, so that can not possible to handle in manage from select box. But you must require to make select dropdown for your products tables then you can do it using select2.js plugin. I already post autocomplete with Bootstrap Typeahead Plugin(Laravel 5 Autocomplete using Bootstrap Typeahead JS Example with Demo), But it is not possible with dropdown like key and value, This post will help to implement select box autocomplete value using select2 js ajax.
In this tutorial, i going to give you full example from scratch so you can easily understand and implement it on your project. You have to just follow few step to done this example. I also give you demo for testing in Part 2. So you can also see the demo on the next part. After finish this example you will see out put like as bellow screenshot:
Preview:
Step 1 : Install Laravel Application
we are going from scratch, So we require to get fresh Laravel 5.3 application using bellow command, So open your terminal OR command prompt and run bellow command:
composer create-project --prefer-dist laravel/laravel blog
Step 2 : Database Configuration
In this step, we require to make database configuration, you have to add following details on your .env file.
1.Database Username
2.Database Password
3.Database Name
In .env file also available host and port details, you can configure all details as in your system, So you can put like as bellow:
.env
DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
Step 3: Create categories Table
In this step we have to create migration for categories table using Laravel 5.3 php artisan command, so first fire bellow command:
php artisan make:migration create_category_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 categories table.
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCategoryTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('categories');
}
}
Now we require to run migration be bellow command:
php artisan migrate
After create "categories" table, we should have some dummy data for testing, So make sure that.
Step 4: Create Route
In this is step we need to create route for form layout file and another one for ajax request. so open your routes/web.php file and add following route.
routes/web.php
Route::get('select2-autocomplete', 'Select2AutocompleteController@layout');
Route::get('select2-autocomplete-ajax', 'Select2AutocompleteController@dataAjax');
Ok Now we will see last two step in next page, so click on bellow button.
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Get Current Week Records in Laravel?
- How to Create Seeder with JSON data in Laravel?
- Laravel Eloquent Left Join Where Null Condition Example
- Laravel Phone Number Verification using Firebase Example
- Laravel Blade Foreach Loop Example
- Laravel Eloquent orderByRaw() Query Example
- Laravel Ajax Request with Validation Example
- PHP Autocomplete Websites with Domain using Clearbit API
- Codeigniter Select2 Ajax Autocomplete from Database Example
- PHP JQuery Select2 Ajax Autocomplete Example
- Laravel 5 Autocomplete using Bootstrap Typeahead JS Example with Demo
- How to Concat Two Columns in Laravel?
- How to Send Mail in PHP Laravel?
- Multi Select Autocomplete JQuery Example with Code