Laravel 10 Dynamic Autocomplete Search with Select2

Last Updated on by in Laravel
This is a comprehensive laravel post; in this tutorial, we will look at how to build a dynamic autocomplete search using the Select2 jQuery plugin in Laravel with AJAX.Let me give you an overview of how we will proceed. We will install a new laravel application, make the database connection and pour some fake movies data into it. Afterward, we will create a model and a controller and bind it to view. Lastly, i will create a view and relentlessly add Bootstrap 5 and implement Select2 JavaScript CDN link to create an autocomplete search.

Install Laravel Application

First and foremost step begins with installing the brand new laravel application. Run the command to sustain the development process.

composer create-project laravel/laravel laravel-autocomplete-search --prefer-dist

Next, get into the project directory:

cd laravel-autocomplete-search

Skip this step, if you have already installed the app.

Configure Database

Add database name, username and password in .env file; it sustains the consensus between laravel and database.


MAMP user may get given below error while running the migration. Incorporate the following code after the database configuration in .env file.


Add some fake data that needs to be searched, go to PHPMyAdmin and run the following SQL query.

INSERT INTO `movies` (`id`, `name`, `created_at`, `updated_at`) VALUES
(1, 'Black Panther', NULL, NULL),
(2, 'Avengers: Endgame', NULL, NULL),
(3, 'Mission: Impossible - Fallout', NULL, NULL),
(4, 'Spider-Man: Into the Spider-Verse', NULL, NULL),
(5, 'Mad Max: Fury Road', NULL, NULL),
(6, 'Wonder Woman', NULL, NULL),
(7, 'Coco', NULL, NULL),
(8, 'Dunkirk', NULL, NULL),
(9, 'Thor: Ragnarok', NULL, NULL),
(10, 'Logan', NULL, NULL);

Model & Migration

Create a Movie Model that is generated by the following model command.

php artisan make:model Movie -m

In database/migrations/timestamp_create_movies_table file add the table values that you want to generate in database.


use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateMoviesTable extends Migration
     * Run the migrations.
     * @return void
    public function up()
        Schema::create('movies', function (Blueprint $table) {

     * Reverse the migrations.
     * @return void
    public function down()

Create the app/Models/Movie.php and add the given values within the $fillable array.


namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Movie extends Model
    use HasFactory;
    protected $fillable = [


Run the following command to run the migration.

php artisan migrate

Create Routes

We need to create two routes. Both the routes simultaneously load the search view and process AJAX dynamic search through select2 jQuery plugin to fetch the data from the database and display it to the user.

Add the below code in routes/web.php file.


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Select2SearchController;

| Web Routes

Route::get('/search', [Select2SearchController::class, 'index']);
Route::get('/ajax-autocomplete-search', [Select2SearchController::class, 'selectSearch']);

Create Autocomplete Search Controller

Create a controller by using the following command.

php artisan make:controller Select2SearchController

In controller file we will create two functions.

The index() method brings the autocomplete search in the view, and the selectSearch() function contains the logic to fetch the data from the database on matched text.

Insert the following code in app\Http\Controllers/Select2SearchController.php.


namespace App\Http\Controllers;
use App\Models\Movie;
use Illuminate\Http\Request;

class Select2SearchController extends Controller

    public function index()
    	return view('home');

    public function selectSearch(Request $request)
    	$movies = [];

            $search = $request->q;
            $movies =Movie::select("id", "name")
            		->where('name', 'LIKE', "%$search%")
        return response()->json($movies);

Create View

Theoretically, we have reached almost the last step of this tutorial. First, create resources/views/home.blade.php file, then add the following code.

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Laravel Ajax Autocomplete Dynamic Search with select2</title>

    <link rel="stylesheet" type="text/css"
    <script src=""></script>
    <link href="" rel="stylesheet" />
    <script src=""></script>

        .container {
            max-width: 500px;
        h2 {
            color: white;


<body class="bg-primary">
    <div class="container mt-5">
        <h2>Laravel AJAX Autocomplete Search with Select2</h2>

        <select class="livesearch form-control" name="livesearch"></select>

<script type="text/javascript">
        placeholder: 'Select movie',
        ajax: {
            url: '/ajax-autocomplete-search',
            dataType: 'json',
            delay: 250,
            processResults: function (data) {
                return {
                    results: $.map(data, function (item) {
                        return {
            cache: true

We import the Bootstrap 5 beta version to use the custom UI component to spruce up the UI. We are also implementing the select2 jQuery plugin CDN link inside the head part of the template.

Test App

Finally, we have gone through each and every imperative respectively. Now, Its time to test out what we have built.

Run the following command to start the laravel app.

php artisan serve

Open the following URL in the browser:

Laravel Dynamic Autocomplete Search with Select2 Example

Digamber - Author

Hi, I'm Digamber Singh, a New Delhi-based full-stack developer, tech author, and open-source contributor with 10+ years' experience in HTML, CSS, JavaScript, PHP, and WordPress.