soraspire/laravel-searchable

A Laravel package that provides a flexible Searchable trait with support for custom operators (like, regex, soundex, fulltext, etc.)

1.0.0 2025-08-28 04:19 UTC

This package is auto-updated.

Last update: 2025-08-28 06:07:48 UTC


README

A lightweight Laravel package that enables advanced searching & sorting directly from URL query parameters. Designed to let Frontend (FE) fully control search logic without touching Backend code. Supports custom operators such as like, regex, soundex, fulltext, and more.

Requirements

  • PHP 8.1 or higher
  • MySQL 8.0+
  • Laravel 10.0+

Features

⚡ Fast attribute search with Eloquent

🎛️ Advanced search from request

🔍 Search through single or multiple columns

🔗 Search through (nested) Relationships

📖 Support for Full-Text Search, Soundex Search

↕️ Supports sorting on attributes and first-level relationships

⚙️ Define your own operators

🛡️ Whitelist allowed columns

Installation

You can install the package via composer:

composer require soraspire/laravel-searchable

Publish Config

php artisan vendor:publish --tag=config --provider="Soraspire\\Searchable\\ServiceProvider"

Usage

In your model:

use Soraspire\Searchable\Traits\Searchable;

class Post extends Model
{
    use Searchable;

    protected array $searchable = [
        'title',
        'content',
        'comments' => ['content'],
        'user'     => ['id', 'name', 'email'],
    ];
}

Now you can search with custom operators:

Post::query()->search();

Search directly from URL query params:

GET /posts?title__like=Laravel

In the above query:

title__like=Laravel → find posts with title containing "Laravel"

Search with multiple columns

Search across multiple columns in the same table by passing multiple query conditions.

GET /posts?title__like=Laravel&content__like=Laravel

Search through relationships

Supports searching through relationships (e.g., comments, user). You can combine conditions from both the main model and related models.

GET /posts?title__like=Laravel&content__like=Laravel&comments__content__en=Laravel&comments__user__name__eq=Laravel

FULLTEXT search

Performs a full-text search, allowing more accurate matching across multiple keywords. Works best on columns with a fulltext index.

GET /posts?content__fulltext=Laravel%20Eloquent

SOUNDEX (raw operator)

Searches using the SOUNDEX operator, which matches words that sound similar. Useful for names or terms with different spellings but similar pronunciation.

GET /posts?content__soundex=Laravel

Custom Operators

You can define your own operators inside the config/searchable.php file.

'today' => ['type' => 'raw', 'sql' => 'DATE({col}) = CURDATE()'],

Now you can search using your custom operators:

GET /posts?content__today

Supported Query Operators

Operator Type SQL / Pattern Description
eq basic = Equal
not_eq basic != Not equal
lt basic < Less than
lte basic <= Less than or equal
gt basic > Greater than
gte basic >= Greater than or equal
cont like %{val}% Contains
start like {val}% Starts with
end like %{val} Ends with
like like {val} Exact like
in in - Value in list
not_in not_in - Value not in list
between between - Value between two values
not_between not_between - Value not between two values
null null - Is null
not_null not_null - Is not null
present present - Field exists
blank blank - Field is blank
year raw YEAR({col}) = ? Match year
month raw MONTH({col}) = ? Match month
date raw DATE({col}) = ? Match date
before_today raw DATE({col}) < CURDATE() Date before today
after_today raw DATE({col}) > CURDATE() Date after today
today raw DATE({col}) = CURDATE() Today
regexp raw {col} REGEXP ? Match regex
not_regexp raw {col} NOT REGEXP ? Not match regex
soundex raw SOUNDEX({col}) = SOUNDEX(?) Match similar sounding words
fulltext raw MATCH({col}) AGAINST (? IN BOOLEAN MODE) Full-text search

Sorting on attributes

You can sort the result attributes using the order query parameter.

GET /posts?order=title__asc

Sorting on first-level relationships

GET /posts?order=title__asc,comments__user_id__desc

License

The MIT License (MIT). Please see License File for more information.