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.1(1mo ago)045MITPHPPHP &gt;=8.2

Since Feb 6Pushed 1mo 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 today

READMEChangelog (3)Dependencies (8)Versions (4)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

38

—

LowBetter than 83% of packages

Maintenance89

Actively maintained with recent releases

Popularity10

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity39

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 ~47 days

Total

3

Last Release

55d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/22852?v=4)[Vitauts Stočka](/maintainers/vits)[@vits](https://github.com/vits)

---

Top Contributors

[![vits](https://avatars.githubusercontent.com/u/22852?v=4)](https://github.com/vits "vits (3 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

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k8.4M96](/packages/mongodb-laravel-mongodb)[kirschbaum-development/eloquent-power-joins

The Laravel magic applied to joins.

1.6k32.6M46](/packages/kirschbaum-development-eloquent-power-joins)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k5.1M35](/packages/tucker-eric-eloquentfilter)[mohammad-fouladgar/eloquent-builder

526198.2k](/packages/mohammad-fouladgar-eloquent-builder)[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.

448199.3k1](/packages/mehdi-fathi-eloquent-filter)

PHPackages © 2026

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