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:
What Typesense is How to set it up in Windows with Docker How to integrate it with Laravel Scout 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:
Real-time search indexing Easy self-hosting via Docker Rich features like synonyms, filtering, and geo-searchStep 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
Plain Bash C++ C# CSS Diff HTML/XML Java JavaScript Markdown PHP Python Ruby SQL 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:
Plain Bash C++ C# CSS Diff HTML/XML Java JavaScript Markdown PHP Python Ruby SQL curl -X GET "http://localhost:8108/health" -H "X-TYPESENSE-API-KEY: xyz"
If successful, it returns:
Plain Bash C++ C# CSS Diff HTML/XML Java JavaScript Markdown PHP Python Ruby SQL {"ok":true}
Step 2. Install Laravel Scout and Typesense Driver In your Laravel project:
Plain Bash C++ C# CSS Diff HTML/XML Java JavaScript Markdown PHP Python Ruby SQL composer require laravel/scout
composer require typesense/typesense-php
Publish Scout config:
Plain Bash C++ C# CSS Diff HTML/XML Java JavaScript Markdown PHP Python Ruby SQL php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
Configure .env
Add:
Plain Bash C++ C# CSS Diff HTML/XML Java JavaScript Markdown PHP Python Ruby SQL 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:
Plain Bash C++ C# CSS Diff HTML/XML Java JavaScript Markdown PHP Python Ruby SQL php artisan make:model Product -m
Update migration:
Plain Bash C++ C# CSS Diff HTML/XML Java JavaScript Markdown PHP Python Ruby SQL 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:
Plain Bash C++ C# CSS Diff HTML/XML Java JavaScript Markdown PHP Python Ruby SQL php artisan migrate
Step 4. Update Product Model for Typesense In Product.php
:
Plain Bash C++ C# CSS Diff HTML/XML Java JavaScript Markdown PHP Python Ruby SQL <?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,
'name' => $this ->name,
'description' => $this ->description,
'price' => (float ) $this ->price,
];
}
}
Step 5. Seed Sample Products Create a factory:
Plain Bash C++ C# CSS Diff HTML/XML Java JavaScript Markdown PHP Python Ruby SQL php artisan make:factory ProductFactory --model=Product
Update ProductFactory.php
:
Plain Bash C++ C# CSS Diff HTML/XML Java JavaScript Markdown PHP Python Ruby SQL public function definition()
{
return [
'name' => $this ->faker->word (),
'description' => $this ->faker->sentence (),
'price' => $this ->faker->randomFloat (2 , 10 , 1000 ),
];
}
Seed data:
Plain Bash C++ C# CSS Diff HTML/XML Java JavaScript Markdown PHP Python Ruby SQL php artisan tinker
Product::factory()->count(20)->create();
Step 6. Import Products into Typesense Run:
Plain Bash C++ C# CSS Diff HTML/XML Java JavaScript Markdown PHP Python Ruby SQL 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
:
Plain Bash C++ C# CSS Diff HTML/XML Java JavaScript Markdown PHP Python Ruby SQL <?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
Plain Bash C++ C# CSS Diff HTML/XML Java JavaScript Markdown PHP Python Ruby SQL <?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
:
Plain Bash C++ C# CSS Diff HTML/XML Java JavaScript Markdown PHP Python Ruby SQL <!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 Visit http://localhost:8000/products/
. Try searching for a product name or description keyword. See results fetched instantly from Typesense.
Conclusion You have now built a complete product search application in Laravel using Typesense . This setup provides:
Blazing-fast search performance Typo tolerance for better user experience Easy scaling when your product catalog grows
VIDEO