Looking to hire Laravel developers? Try LaraJobs

laravel-smartsearch maintained by aghfatehi

Description
Laravel SmartSearch by Fatehi AL-AGHBARI — production-grade search abstraction layer for Laravel with Elasticsearch, Scout, and database fallback. Unified search API, automatic indexing, Arabic/multilingual support, queue-driven, safe fallback. The most flexible Laravel search package for Eloquent models.
Last update
2026/06/07 17:03 (dev-main)
License
Links
Downloads
0

Comments
comments powered by Disqus

What is Laravel SmartSearch?

Laravel SmartSearch is a powerful, production-ready Laravel search package that provides a unified search API across Elasticsearch, Laravel Scout, and database full-text search — a true Laravel Scout alternative with more flexibility. Built by Fatehi AL-AGHBARI, it solves the common pain points teams face when adding search to Laravel applications — complex setup, vendor lock-in, and brittle fallback logic.

Whether you need Elasticsearch for Laravel, a Laravel Scout alternative, or database search for Laravel, SmartSearch gives you one clean Laravel search API that works everywhere. The Eloquent search layer integrates directly with your models for zero-friction indexing.


Why Laravel SmartSearch?

Integrating search for Laravel applications often means:

  • Hard-coding Elasticsearch queries
  • Breaking changes when switching providers
  • No safety net when the search engine goes down
  • Painful multilingual setup (especially Arabic)

SmartSearch fixes all of this:

Problem Traditional Approach SmartSearch
Setup complexity Hours of config 1-minute setup
Vendor lock-in Tied to one engine Switch drivers anytime
No fallback 503 errors Auto database fallback
Arabic search Custom hacks Built-in normalizer
Indexing overhead Manual sync Auto queue-driven

Features

  • Unified Laravel search APISearch::for(Model::class)->query('term')->get()
  • Full text search for Laravel — works out of the box with database search, no extra packages
  • Eloquent search — trait-based, integrates directly with your models
  • Database search LaravelLIKE / ILIKE auto-detects MySQL, PostgreSQL, SQLite, SQL Server
  • Elasticsearch for Laravel — high-performance dedicated search, install only if needed
  • Laravel Scout alternative — drop-in compatible, works with Algolia, MeiliSearch, Typesense
  • Search indexing — queue-driven on model created/updated/deleted
  • Automatic safe fallback — if Elasticsearch/Scout is down, falls back to database
  • Arabic search for Laravel — built-in normalization for Arabic content
  • Multilingual search — configurable analyzers for any language
  • Driver-based architecture — Strategy Pattern, extensible
  • Works with any database — MySQL, PostgreSQL, SQLite, SQL Server, no breaking changes
  • Laravel 9 / 10 / 11 / 12 / 13 — full backward compatibility

Installation

No extra dependencies required. This Laravel search package works immediately with database search.

composer require aghfatehi/laravel-smartsearch

Publish the config (optional):

php artisan vendor:publish --tag=smartsearch-config

Optional drivers: To use Elasticsearch for Laravel, run composer require elasticsearch/elasticsearch. To use Laravel Scout, run composer require laravel/scout. The package handles both gracefully without breaking.


Quick Setup

1. Add the trait to your model

use SmartSearch\Traits\Searchable;

class Product extends Model
{
    use Searchable;

    protected $searchable = ['name', 'description', 'price'];
}

2. (Optional) Configure your driver

The default driver is database — works immediately with your existing database. To switch to Elasticsearch:

SMARTSEARCH_DRIVER=elasticsearch
SMARTSEARCH_FALLBACK=database

That's it.


Usage

Basic search

use SmartSearch\Facades\Search;

$results = Search::for(Product::class)
    ->query('iphone')
    ->get();

Search with filters

$results = Search::for(Product::class)
    ->query('iphone')
    ->where('price', '<', 5000)
    ->where('brand', 'Apple')
    ->get();

Paginated search

$results = Search::for(Product::class)
    ->query('laptop')
    ->paginate(20);

Eloquent-style shortcut

Product::search('iphone')->get();

Helper function

smartSearch(Product::class, 'iphone')->get();

Auto Indexing

SmartSearch hooks into Eloquent events to keep your search index in sync automatically:

Event Action
created Queue IndexDocument job
updated Queue IndexDocument job
deleted Queue DeleteDocument job

All operations are queue-based (non-blocking) and configurable.


Safe Database Fallback

When your primary search engine (Elasticsearch / Scout) is unavailable:

// No special error handling needed
$results = Search::for(Product::class)->query('iphone')->get();
// Automatically falls back to database search

The database driver auto-detects your database engine:

Database LIKE Operator Case-Insensitive
MySQL LIKE Depends on collation
PostgreSQL ILIKE Always
SQLite LIKE ASCII
SQL Server LIKE Depends on collation

Arabic & Multilingual Search

Full Arabic search for Laravel and multilingual search support out of the box:

use SmartSearch\Support\ArabicNormalizer;

ArabicNormalizer::normalize('مُحَمَّد'); // محمد
ArabicNormalizer::normalize('أحمد إبراهيم آدم'); // احمد ابراهيم ادم

Configurable Elasticsearch analyzer for any language:

// config/smartsearch.php
'elasticsearch' => [
    'analyzer' => 'arabic', // or 'standard', 'english', custom, etc.
],

Driver System

Driver When to use
DatabaseDriver (default) Works out of the box — no extra packages needed
ElasticsearchDriver High-performance, dedicated search infrastructure
ScoutDriver Already using Laravel Scout (Algolia, MeiliSearch, Typesense)

Configuration

// config/smartsearch.php
return [
    'driver' => env('SMARTSEARCH_DRIVER', 'elasticsearch'),
    'fallback' => env('SMARTSEARCH_FALLBACK', 'database'),
    'queue' => env('SMARTSEARCH_QUEUE', true),
    'connection' => env('SMARTSEARCH_CONNECTION', 'default'),
    'elasticsearch' => [
        'hosts' => explode(',', env('ELASTICSEARCH_HOSTS', 'localhost:9200')),
        'analyzer' => env('ELASTICSEARCH_ANALYZER', 'standard'),
    ],
    'index_prefix' => env('SMARTSEARCH_INDEX_PREFIX', ''),
];

Architecture

Built on clean architecture principles:

  • Strategy Pattern for drivers
  • Service Container binding
  • Event-driven indexing
  • SOLID principles throughout
Laravel Application
    |
    v
Laravel SmartSearch Layer
    |
    v
+---------------------------------------------+
| DatabaseDriver (default - works OOB)        |
| ElasticsearchDriver (optional high-perf)    |
| ScoutDriver (optional - Algolia, Meili...)  |
+---------------------------------------------+
| Automatic safe fallback between drivers     |
+---------------------------------------------+

Use Cases

  • E-commerce search — product search with filters and faceting
  • ERP systems — search across invoices, customers, orders
  • SaaS platforms — multi-tenant search (coming soon)
  • Real estate listings — full-text property search
  • Document management — search titles, descriptions, content
  • Arabic content platforms — normalized Arabic search for Laravel

Requirements

  • PHP 8.1+
  • Laravel 9.x / 10.x / 11.x / 12.x / 13.x
  • Database MySQL / PostgreSQL / SQLite / SQL Server (any Laravel-supported database)
  • Elasticsearch 7.x+ (only if using the Elasticsearch driver — optional)

Contributing

  1. Fork the repo
  2. Create a feature branch (git checkout -b feature/my-feature)
  3. Commit your changes (git commit -m 'Add my feature')
  4. Push (git push origin feature/my-feature)
  5. Open a Pull Request

Support

If this Laravel search package helps you:

  • Star the repository on GitHub
  • Report issues
  • Share with the Laravel community
  • Looking for an Elasticsearch Laravel or database search solution? This is it.

License

MIT License. Free for personal and commercial use.