PHPackages                             nld-labs/laravel-search - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. nld-labs/laravel-search

ActiveLibrary[Database &amp; ORM](/categories/database)

nld-labs/laravel-search
=======================

Search trait for Laravel Eloquent models with multiple search strategies.

0.2.0(3mo ago)08MITPHPPHP &gt;=8.2

Since Feb 6Pushed 3mo agoCompare

[ Source](https://github.com/nld-labs/laravel-search)[ Packagist](https://packagist.org/packages/nld-labs/laravel-search)[ RSS](/packages/nld-labs-laravel-search/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (4)Versions (3)Used By (0)

Laravel Search
==============

[](#laravel-search)

[![Latest Version on Packagist](https://camo.githubusercontent.com/84ff7229508073a45bf3f11a1b8becc2c62a096743953428c63fc3147503d37a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6e6c642d6c6162732f6c61726176656c2d7365617263682e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/nld-labs/laravel-search)

A search trait for Laravel Eloquent models with multiple search strategies. Splits the search term into words, matches each word against every specified field, and supports per-field strategy selection with database-aware query building.

Requirements
------------

[](#requirements)

- PHP ≥ 8.2
- Laravel 11 or 12

Installation
------------

[](#installation)

```
composer require nld-labs/laravel-search
```

Quick Start
-----------

[](#quick-start)

Add the `Searchable` trait to any Eloquent model:

```
use NLD\Search\Searchable;
use NLD\Search\SearchStrategy;

class Post extends Model
{
    use Searchable;
}
```

Then call the `search` scope:

```
Post::search('laravel auth', 'title,body')->get();
```

Search Strategies
-----------------

[](#search-strategies)

StrategyBehaviourSQL`START_OF_WORDS`Matches the term at the beginning of any word in the field. Uses `~*` on PostgreSQL, `REGEXP` on MySQL/MariaDB, falls back to `LIKE` on SQLite.`"field" ~* '\yterm'``IN_WORDS`Matches the term anywhere inside the field value.`"field" LIKE '%term%'``START_OF_STRING`Matches the term at the very beginning of the field value.`"field" LIKE 'term%'``EXACT`Matches the term exactly.`"field" = 'term'`The default strategy is `START_OF_WORDS`.

Specifying Fields
-----------------

[](#specifying-fields)

### Comma-separated string

[](#comma-separated-string)

All fields use the default strategy (`START_OF_WORDS`, or the model's `$searchStrategy` property):

```
Post::search('term', 'title,body')->get();
```

### Array with explicit strategies

[](#array-with-explicit-strategies)

```
Post::search('term', [
    'title' => SearchStrategy::START_OF_WORDS,
    'body'  => SearchStrategy::IN_WORDS,
    'slug'  => SearchStrategy::START_OF_STRING,
    'code'  => SearchStrategy::EXACT,
])->get();
```

### Mixed array

[](#mixed-array)

Fields without a key use the default strategy:

```
Post::search('term', [
    'title',                              // uses default strategy
    'body' => SearchStrategy::IN_WORDS,   // explicit strategy
])->get();
```

### Dot notation

[](#dot-notation)

Dot-notation field names are wrapped as qualified column references:

```
Post::search('term', [
    'posts.title' => SearchStrategy::IN_WORDS,
])->get();
```

Model Configuration
-------------------

[](#model-configuration)

You can set default searchable fields and a default strategy directly on the model:

```
use NLD\Search\Searchable;
use NLD\Search\SearchStrategy;

class Post extends Model
{
    use Searchable;

    protected array $searchable = [
        'title' => SearchStrategy::START_OF_WORDS,
        'body'  => SearchStrategy::IN_WORDS,
    ];

    // Optional: override the default strategy for fields passed without one
    protected SearchStrategy $searchStrategy = SearchStrategy::IN_WORDS;
}
```

When `$searchable` is defined, calling `search` without fields uses it automatically:

```
Post::search('term')->get();
```

Fields passed explicitly always take precedence over the model property.

How It Works
------------

[](#how-it-works)

1. The search term is split into words (whitespace-separated, max 10 words).
2. Each word produces a `WHERE` clause (ANDed together — every word must match).
3. Inside each clause, every field produces an `OR` condition — matching any field is enough.
4. Field names are validated against `[a-zA-Z_][a-zA-Z0-9_.]*`; invalid names are silently skipped.
5. LIKE wildcards (`%`, `_`, `\`) and regex metacharacters are properly escaped.

Testing
-------

[](#testing)

```
./vendor/bin/pest
```

License
-------

[](#license)

MIT © [Vitauts Stočka](mailto:vs@vits.lv). See [LICENSE](LICENSE) for details.

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance82

Actively maintained with recent releases

Popularity6

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity37

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~4 days

Total

2

Last Release

91d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a7080d68b08c41a268daab0de9e121cd06f79925fdb11b696be38d7ea3fbb170?d=identicon)[vits](/maintainers/vits)

---

Top Contributors

[![vits](https://avatars.githubusercontent.com/u/22852?v=4)](https://github.com/vits "vits (2 commits)")

---

Tags

searchlaraveleloquent

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/nld-labs-laravel-search/health.svg)

```
[![Health](https://phpackages.com/badges/nld-labs-laravel-search/health.svg)](https://phpackages.com/packages/nld-labs-laravel-search)
```

###  Alternatives

[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[mehdi-fathi/eloquent-filter

Eloquent Filter adds custom filters automatically to your Eloquent Models in Laravel.It's easy to use and fully dynamic, just with sending the Query Strings to it.

450191.6k1](/packages/mehdi-fathi-eloquent-filter)[jedrzej/searchable

Searchable trait for Laravel's Eloquent models - filter your models using request parameters

127259.1k5](/packages/jedrzej-searchable)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
