Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Laravel Customized Autocomplete JQuery UI

Laravel Customized Autocomplete JQuery UI

Aman Jain, July 12, 2022July 12, 2022

Laravel Customized Autocomplete JQuery UI is useful when we want live search of bulk data. Autocomplete search is mostly work of javascript and when we want to fetch live data from database then we require the intervention of laravel to provide the data in json response. In this tutorial we will use jQuery ui to show the the autocomplete list on typing.

jQuery UI library is a collection of user interface interactions, effects and widgets. Using the jQuery UI we can also create date picker, drag and drop, select, sort etc.

In this example we will create a simple text box to search on type and give a list suggestion based on typed term. then in laravel we will create an api to response the result of search.

This example will work with any version of laravel like laravel 5, laravel 6, laravel 7, laravel 8 and laravel 9 too.

Let start the simple tutorial of Laravel Customized Autocomplete JQuery UI step by step

Step 1: Create a laravel project

First step is to create the Laravel project using composer command or you can also read the How to install laravel 8 ? and Laravel artisan command to generate controllers, Model, Components and Migrations

composer create-project laravel/laravel crud

Step 2: Configure database

Next step is to configure the database for our project, table and data. So open .env or if file not exist then rename .env.example file to .env file in root folder and change below details

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=

Step 3: Create Model and Migration

If you have table and large data already then you can skip this step but if you are beginner and trying to implement autosuggestion then in this step we are creating a model and migration using artisan command

php artisan make:model User -m

Step 4 : Create a controller

Next, Create a controller and two methods one for view and another for search the suggestion as follow

php artisan make:controller UserController

Now, add the controller logic

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{
    function index(){
        return view("users.search");
    }
    function search(Request $request){

        return  \App\Models\User::where("email","like","%{$request->term}%")
        ->select("id","email","name")
        ->limit(10)
        ->get();
    }
}

Here we created two methods index for show the view and search method for response the related users list according to keyword.

Step 5 : Create the view

Now, create a view file named as resources/views/users/search.blade.php and put the follow code

<!DOCTYPE html>
<html>

<head>
  <title>Laravel Autocomplete JQuery UI Search - readerstacks.com</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.13.1/themes/base/jquery-ui.css">
</head>

<body>
  <div class="container">

    <div class="panel panel-primary">
      <div class="panel-heading">
        <h2>Laravel Autocomplete JQuery UI Search -readerstacks.com</h2>
      </div>
      <div class="panel-body">

        <div class="col-md-4">
          <label>Title</label>
          <input type="text" placeholder="Users" name="users" id='users' class="form-control">
        </div>

      </div>
    </div>
  </div>
</body>

</html>
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
<script src="https://code.jquery.com/ui/1.13.1/jquery-ui.js"></script>
<script>
  $(function() {

    $("#users").autocomplete({
      source: "{{url('users/search')}}",
      minLength: 1,
      select: function(event, ui) {
        setTimeout(() => {
          $("#users").val(ui.item.email)
        }, 0)

      }
    }).autocomplete("instance")._renderItem = function(ul, item) {
      return $("<li>")
        .append("<div>" + item.name + "<br> <span style='color:red'>" + item.email + "</span></div>")
        .appendTo(ul);
    };

  });
</script>

Now, here we imported the jQuery and jQuery UI js and CSS to implement the suggestion list autocomplete

Customized suggestion list of jQuery UI

Sometimes we want to customize the result of suggestion so we will also cover that in this article. We used the below code to customize the list data as follow

 $(function() {

    $("#users").autocomplete({
      source: "{{url('users/search')}}",
      minLength: 1,
      select: function(event, ui) {
        setTimeout(() => {
          $("#users").val(ui.item.email)
        }, 0)

      }
    }).autocomplete("instance")._renderItem = function(ul, item) {
      return $("<li>")
        .append("<div>" + item.name + "<br> <span style='color:red'>" + item.email + "</span></div>")
        .appendTo(ul);
    };

  });

Step 6 : Create Route

Last and final step is to create the route for our implementation

<?php

use Illuminate\Support\Facades\Route;

Route::get('/users',"\App\Http\Controllers\UserController@index");
Route::get('/users/search',"\App\Http\Controllers\UserController@search");

Live Screenshot :

laravel autocomplete jquery ui
laravel autocomplete jquery ui

Related

Javascript jQuery Laravel Laravel 9 Php ajaxautocompletecustomizedjqueryuilaravelsuggestion list

Post navigation

Previous post
Next post

Related Posts

Php Laravel 9 Ajax CRUD with Search, Image and Pagination

Laravel 9 Ajax CRUD with Search, Image and Pagination

June 26, 2022March 28, 2023

In this article i will learn Ajax CRUD with Search, Image and Pagination in laravel 9. When we want to perform the CRUD operations without reloading the page then we use javascript and Ajax to refresh the page content without reloading.In this post we will not only cover the Ajax…

Read More
Laravel Efficient and Secure Way to Write Raw Queries in Laravel

Efficient and Secure Way to Write Raw Queries in Laravel

July 1, 2024July 1, 2024

Laravel is a powerful and versatile PHP framework that simplifies many aspects of web development, including database interactions. While Laravel’s Eloquent ORM and query builder are incredibly useful, there are times when you need to write raw SQL queries for more complex operations. However, writing raw queries requires a careful…

Read More
Php How to Check folder exist and Create a Nested Folder in Laravel

How to Check folder exist and Create Nested Folder in Laravel ?

May 24, 2022May 26, 2022

This article will guide you to Check folder exist and Create Nested Folder in Laravel. To perform the file system based operations like Check folder exist and Create a Nested Folder. we use File class in laravel. Laravel provides inbuilt library to access the file system and we can do…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version