Laravel Scout With Typesense Build A Full Product Search Example

image
image
image
image
image
image
image
image
Laravel Scout with Typesense: Build a Full Product Search Example

Laravel Scout with Typesense: Build a Full Product Search Example

Hello, in this blog we will see how you implement typesense with laravel, if you want a full information over how you can setup typesense with the window PC please check this URL - https://www.codehunger.in/blog/run-typesense-locally-in-windows-pc

Introduction

Fast and typo-tolerant search is a vital feature in modern applications. Laravel Scout simplifies integration with search engines, and Typesense is an excellent open-source choice for fast, typo-tolerant search.

In this tutorial, you will learn:

  1. What Typesense is
  2. How to set it up in Windows with Docker
  3. How to integrate it with Laravel Scout
  4. How to build a complete product search page using Blade

What is Typesense?

Typesense is a fast, open-source search engine built for performance, typo-tolerance, and developer productivity. It provides:

  1. Real-time search indexing
  2. Easy self-hosting via Docker
  3. Rich features like synonyms, filtering, and geo-search

Step 1. Install Typesense Locally Using Docker

First, ensure Docker is installed on your Windows PC. Then, open PowerShell or CMD and run, before running the below command please make sure you have installed the docker up and running

docker run -d -p 8108:8108 -v C:\typesense-data:/data typesense/typesense:29.0 typesense-server --data-dir /data --api-key=xyz --enable-cors

Replace xyz with a strong API key.

Test Typesense is running

Run:

curl -X GET "http://localhost:8108/health" -H "X-TYPESENSE-API-KEY: xyz"

If successful, it returns:

{"ok":true}

Step 2. Install Laravel Scout and Typesense Driver

In your Laravel project:

composer require laravel/scout
composer require typesense/typesense-php

Publish Scout config:

php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"

Configure .env

Add:

SCOUT_DRIVER=typesense
TYPESENSE_API_KEY=xyz
TYPESENSE_HOST=localhost
TYPESENSE_PORT=8108
TYPESENSE_PATH=
TYPESENSE_PROTOCOL=http

Replace xyz with your Typesense API key.

Step 3. Create Product Model and Migration

Generate:

php artisan make:model Product -m

Update migration:

public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('description')->nullable();
$table->float('price');
$table->timestamps();
});
}

Run migration:

php artisan migrate

Step 4. Update Product Model for Typesense

In Product.php:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Laravel\Scout\Searchable;

class Product extends Model
{
use HasFactory, Searchable;

protected $guarded = [];

public function toSearchableArray()
{
return [
'id' => (string) $this->id, // Typesense requires string id
'name' => $this->name,
'description' => $this->description,
'price' => (float) $this->price,
];
}
}

Step 5. Seed Sample Products

Create a factory:

php artisan make:factory ProductFactory --model=Product

Update ProductFactory.php:

public function definition()
{
return [
'name' => $this->faker->word(),
'description' => $this->faker->sentence(),
'price' => $this->faker->randomFloat(2, 10, 1000),
];
}

Seed data:

php artisan tinker
Product::factory()->count(20)->create();

Step 6. Import Products into Typesense

Run:

php artisan scout:import "App\Models\Product"

This indexes your products in Typesense for fast search.

Step 7. Create a Product Search Route and Controller

In web.php:

<?php

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



Route::get('/products', [ProductController::class, 'index']);
Route::get('/products/search', [ProductController::class, 'search']);

Step 8. Create a Product Controller

run this command PHP artisan make:controller ProductController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Product;

class ProductController extends Controller
{
public function index()
{
$products = Product::all();
return view('products.index', compact('products'));
}

public function search(Request $request)
{
$query = $request->input('query');
$products = Product::search($query)->get();
return view('products.index', compact('products'));
}
}


Step 8. Create Blade View

Create resources/views/products/index.blade.php:

<!DOCTYPE html>
<html>
<head>
<title>Product Search with Typesense</title>
</head>
<body>
<h1>Products</h1>

<form action="/products/search" method="GET">
<input type="text" name="query" placeholder="Search products..." required>
<button type="submit">Search</button>
</form>

<ul>
@forelse($products as $product)
<li>
<strong>{{ $product->name }}</strong><br>
{{ $product->description }}<br>
₹{{ $product->price }}
</li>
@empty
<li>No products found.</li>
@endforelse
</ul>
</body>
</html>

Step 9. Test Your Search

  1. Visit http://localhost:8000/products/.
  2. Try searching for a product name or description keyword.
  3. See results fetched instantly from Typesense.


Conclusion

You have now built a complete product search application in Laravel using Typesense. This setup provides:

  1. Blazing-fast search performance
  2. Typo tolerance for better user experience
  3. Easy scaling when your product catalog grows

You can also download the repository from the github - https://github.com/codehunger-team/laravel-scout-with-typesense