sharifuddin/laravel-smart-search

A smart search helper for Laravel. Provides advanced Eloquent search with relation discovery and multiple search modes.

Installs: 8

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/sharifuddin/laravel-smart-search

v1.1.0 2025-11-01 10:54 UTC

This package is auto-updated.

Last update: 2025-11-02 16:59:27 UTC


README

Laravel Smart Search is a high-performance, intelligent search package for Laravel Eloquent models. It provides automatic relation discovery, configurable search depth, multiple search modes, and optimized queries for large datasets.

โœจ Key Features

  • ๐Ÿ” Smart Relation Discovery โ€“ Automatically searches through model relationships.
  • โšก High Performance โ€“ Optimized queries with configurable limits.
  • ๐ŸŽฏ Multiple Search Modes โ€“ like, exact, starts_with, ends_with.
  • ๐Ÿ”ง Fully Configurable โ€“ Customize search behavior and priorities.
  • ๐Ÿ“š Priority Columns โ€“ Define columns to prioritize for better results.
  • ๐Ÿ›ก๏ธ Type Safe โ€“ Full type hints, PHPStan ready.
  • ๐Ÿงช Fully Tested โ€“ Comprehensive test coverage included.
  • ๐Ÿ”Œ Laravel Native โ€“ Seamless integration with Eloquent models.

๐Ÿ“ฆ Installation

Requirements

  • PHP 8.1+
  • Laravel 10.x or 11.x

Via Composer

composer require sharif/laravel-smart-search

``

Publish Configuration (Optional)

php artisan vendor:publish --provider="Sharifuddin\\LaravelSmartSearch\\SmartSearchServiceProvider" --tag="smart-search-config"

๐ŸŽฏ Quick Start

1๏ธโƒฃ Use the Trait

Add the SmartSearch trait to your Eloquent models:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Sharifuddin\LaravelSmartSearch\Traits\SmartSearch;

class Product extends Model
{
    use SmartSearch;
}

2๏ธโƒฃ Basic Search Usage

// Simple search
$products = Product::applySmartSearch('laptop')->get();

// Paginated search
$products = Product::applySmartSearch($request->input('search'))->paginate(15);

// Search with relations
$products = Product::applySmartSearch('apple')->with('brand', 'categories')->get();

๐Ÿ”ง Configuration

After publishing, edit config/smart-search.php to customize behavior:

return [
    'defaults' => [
        'mode' => 'like',
        'deep' => true,
        'max_relation_depth' => 2,
        'search_operator' => 'or',
    ],

    'columns' => [
        'excluded' => [
            'id', 'created_at', 'updated_at', 'deleted_at',
            'password', 'remember_token', 'email_verified_at'
        ],
        'prioritized' => ['name', 'title', 'code', 'email'],
        'max_per_table' => 10,
    ],

    'relations' => [
        'auto_discover' => true,
        'max_depth' => 2,
        'excluded' => ['password', 'secret', 'tokens'],
    ],

    'performance' => [
        'max_join_tables' => 5,
        'chunk_search' => false,
        'chunk_size' => 1000,
    ],
];

๐Ÿ’ก Usage Examples

Basic Search

$users = User::applySmartSearch('john')->get();
$products = Product::applySmartSearch('wireless keyboard')->paginate(20);

Advanced Search with Options

$results = Product::applySmartSearch(
    search: 'laptop',
    columns: ['name', 'sku', 'description'],
    options: [
        'mode' => 'like',
        'deep' => true,
        'max_relation_depth' => 1
    ]
)->get();

Relation Search

$posts = Post::applySmartSearch('laravel tips')->with('author', 'tags', 'comments')->get();

Multiple Search Modes

// Like (default)
$results = Product::applySmartSearch('phone');

// Exact match
$results = User::applySmartSearch('admin@example.com', [], ['mode' => 'exact']);

// Starts with
$results = Product::applySmartSearch('APP', [], ['mode' => 'starts_with']);

// Ends with
$results = Product::applySmartSearch('001', [], ['mode' => 'ends_with']);

โšก Performance Optimization

  • Add indexes for frequently searched columns.
  • Limit max_relation_depth for large datasets.
  • Disable deep option for simple queries.
  • Enable chunk_search in config for large tables.

๐Ÿงช Testing

# Run all tests
composer test

# Run tests with coverage
composer test-coverage

๐Ÿค Contributing

  1. Fork the repository.
  2. Clone your fork: git clone https://github.com/your-username/laravel-smart-search.
  3. Install dependencies: composer install.
  4. Run tests: composer test.

See CONTRIBUTING.md for more details.

๐Ÿ“„ License

MIT License. See LICENSE.md.

Laravel Smart Search - Intelligent search for intelligent applications.

GitHub โ€ข Packagist โ€ข Issues โ€ข Discussions

```