How to Create AJAX Autocomplete Search in Laravel 8 with Select2

In this tutorial you will learn about the How to Create AJAX Autocomplete Search in Laravel 8 with Select2 and its application with practical example.

In this How to Create AJAX Autocomplete Search in Laravel 8 with Select2 tutorial, I will show you how to create dynamic ajax driven autocomplete with select2 in laravel. In this tutorial you will learn to create dynamic ajax driven autocomplete with select2 in laravel. In this example we will be using Select2 jQuery plugin in Laravel 8 with AJAX.

How to Create AJAX Autocomplete Search in Laravel 8 with Select2

The jquery Select2 plugin is used to create dynamic autocomplete input with several options. In this example you will learn how to implement Select2 autocomplete in laravel.

  1. Install Laravel 8 Application
  2. Database Configuration
  3.  Create product Table and Model
  4. Create Routes
  5. Create Controller
  6. Create View

Step 1 : Install Laravel Application

First of all we need to create a fresh laravel project, download and install Laravel using the below command

Step  2: Database Configuration

Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.

Step 3: Create product Table and Model

Now, in this step we will create model and migration file. Please run the following command:

After this command you have to put bellow code in your migration file for create product table.

/database/migrations/2020_03_05_100722_create_product_table.php

Now, in this step we will create model and migration file. Please run the following command:

After you have to put bellow code in your model file for create product table.

/app/Product.php

Step 4: Create Route

After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.

routes/web.php

Step 5: Create Controller

Now, lets create a controller named Select2AutocompleteController using command given below –

In this controller will add following two methods:

1)index()

2)dataAjax()

Now, put the following code in ItemController.php file. /app/Http/Controllers/Select2AutocompleteController.php

Step 6: Create View

In this step we will create laravel blade view file as following:

/resources/views/select2/index.blade.php

Run Development Server

Now we are ready to run our example so lets start the development server using following artisan command –

Now, open the following URL in browser to see the output –

In this tutorial we have learn about the How to Create AJAX Autocomplete Search in Laravel 8 with Select2 and its application with practical example. I hope you will like this tutorial.