Laravel 9 Autocomplete Search Example with Typeahead
Hello Artisan,
In this Laravel 9 typeahead search example, I will discuss how to search data from databases in Laravel using typeheadjs. In this example, I am going to write an example of how to create autocomplete search using laravel 9 typeahead js. You can implement it laravel 6, laravel 7, laravel 8 and laravel 9 application.
You know that Bootstrap Typeahead JS provides a way of the user interface so, we can easily write the code of jquery ajax and make it dynamic autocomplete search in laravel application. we can easily use Typeahead JS with bootstrap.
Jquery autocomplete is a must if you are dealing with big data like you have items or products table and thousands of records so it's not possible to give a drop-down box, but it is better if we use autocomplete instead of the select box.
So if you don't know how to create autocomplete search using typehead js then this example is for you. Just you have to follow some steps to complete. See the preview from the below image of what we are going to make:
Step 1 : Download Laravel
In this first step, we need to get a fresh Laravel 9 version 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: Create Migration
In this step, we need to create migration for items table using Laravel PHP artisan command, so first execute the bellow command:
php artisan make:model Item -m
And update the migration like:
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateItemsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('items', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('items');
}
}
Now run php artisan migrate
to migrate the database. Now update the Item model like:
App\Models\Item.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Item extends Model
{
protected $guarded = [];
}
Read also: Create Reusable Trait to Generate and Save uuid in Laravel
Step 3: Add Route
In this is step we have to create routes for display view and ajax call. so open routes/web.php
file and add following route.
routes/web.php
use App\Http\Controllers\SearchController;
use Illuminate\Support\Facades\Route;
Route::controller(SearchController::class)->group(function () {
Route::get('/search', 'index')->name('search');
Route::get('/autocomplete', 'autocomplete')->name('autocomplete');
});
Step 4: Create Controller
In this step, we need to create a controller as SearchController and we need to add two methods index() and autocomplete() on that controller like as you can see bellow:
app/Http/Controllers/SearchController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Item;
class SearchController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('search');
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function autocomplete(Request $request)
{
$data = Item::select("name")
->where("name","LIKE","%{$request->query}%")
->get();
return response()->json($data);
}
}
Step 5: Create Blade File
We are in the last step, we just need to create blade file. So create it like below:
resources/views/search.blade.php
Read also: Laravel 9 Multiple Authentication Example using Guard
Hope this tutorial can help you.