PHPackages                             jabbtech/searchable - 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. jabbtech/searchable

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

jabbtech/searchable
===================

Eloquent model search trait.

v9.0(4y ago)1411MITPHPPHP ^7.2|^8.0

Since Jun 17Pushed 4y ago1 watchersCompare

[ Source](https://github.com/curtisweeks/searchable)[ Packagist](https://packagist.org/packages/jabbtech/searchable)[ RSS](/packages/jabbtech-searchable/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (6)Dependencies (1)Versions (57)Used By (0)

Laravel Searchable
==================

[](#laravel-searchable)

Laravel trait to add a `search` method for Eloquent which performs prioritized searches in a model.

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

[](#installation)

```
$ composer require "jabbtech/searchable": "^3.0"

```

Configuration
-------------

[](#configuration)

Import the `SearchableTrait` class and add a `$searchable` property to your model to specify the columns to make searchable and their priority in search results. Columns with higher values are more important:

```
namespace App;

use Illuminate\Database\Eloquent\Model;
use Jabbtech\Searchable\SearchableTrait;

class User extends Model
{

    use SearchableTrait;

    protected $searchable = [
        'columns' => [
            'users.first_name' => 10,
            'users.last_name' => 10,
            'users.bio' => 2,
            'users.email' => 5,
            'posts.title' => 2,
            'posts.body' => 1,
        ],
        'joins' => [
            'posts' => ['users.id','posts.user_id'],
        ],
    ];

    public function posts()
    {
        return $this->hasMany('App\Post');
    }

}
```

Basic Usage
-----------

[](#basic-usage)

Using the `search` method on a query builder instance builds a query that searches through your model using Laravel's Eloquent. The most basic call to `search` requires a single argument, a search string.

```
$users = User::search($query)->get();
```

The `search` method should be compatible with any eloquent method (with exceptions for [SQL Server](#sql-server)):

```
$users = User::search($query)->with('posts')->get();

$users = User::search($query)->with('posts')->paginate(20);

$users = User::search($query)->where('status', 'active')->take(10)->get();
```

#### Accepted Relevance Threshold

[](#accepted-relevance-threshold)

The second argument changes is an integer that can change the default threshold for accepted relevance. By default, the threshold for accepted relevance is the sum of all attribute relevance divided by 4.

Returning all users matching the search in order of relevance:

```
$users = User::search($query, 0)->get();
```

#### Prioritize Full Text Search

[](#prioritize-full-text-search)

The third argument is a boolean which prioritizes matches that include the multi-word search. By default, multi-word search terms are split and Searchable searches for each word individually. Relevance plays a role in prioritizing matches that matched on multiple words.

Prioritizing matches containing "John Doe" above matches containing only "John" or "Doe":

```
$users = User::search("John Doe", null, true)->get();
```

#### Full Text Matches Only

[](#full-text-matches-only)

The fourth argument is a boolean which allows searching for full text mathces only.

Excluding matches that only matched "John" OR "Doe".

```
$users = User::search("John Doe", null, true, true)->get();
```

---

SQL Server
----------

[](#sql-server)

When used with SQL Server the `search` method supports most eloquent methods with some limitations.

- It is strongly recommended (and required in some cases) that the `select` method is used
    - Column constraints of tables you will join to need to be included in the `select` method
    - Column required by the `where` method need to be included in the `select` method
- Most methods need to be used *before* the `search` method
    - `join`
    - `leftJoin`
    - `crossJoin`
    - `where`
- Include `orderBy('relevance', 'desc')` *after* the `search` method

```
$users = User::select('first_name', 'last_name')
    ->search($query)
    ->orderBy('relevance', 'desc')
    ->get();

$users = User::select(
        'users.id',
        'users.first_name',
        'users.last_name',
        'users.bio',
        'users.email',
        'users.status',
        'posts.title',
        'posts.body'
    )->leftJoin('posts', 'users.id', 'posts.user_id')
    ->where('users.status', 'active')
    ->serch($query)
    ->orderBy('relevance', 'desc')
    ->get();
```

---

How does it work?
-----------------

[](#how-does-it-work)

Searchable builds a query that searches through your model using Laravel's Eloquent. Here is an example query

#### Model

[](#model)

```
protected $searchable = [
    'columns' => [
        'first_name' => 10,
        'last_name' => 5
    ],
];
```

#### Search

[](#search)

```
$search = User::search('John Doe', null, true)->get();
```

#### Result

[](#result)

```
select * from (
    select `users`.*, max(
        (case when LOWER(`users`.`first_name`) LIKE 'John' then 150 else 0 end) + -- column equals word: priority * 15
        (case when LOWER(`users`.`first_name`) LIKE 'Doe' then 150 else 0 end) +
        (case when LOWER(`users`.`first_name`) LIKE 'John%' then 50 else 0 end) + -- column starts with word: priority * 5
        (case when LOWER(`users`.`first_name`) LIKE 'Doe%' then 50 else 0 end) +
        (case when LOWER(`users`.`first_name`) LIKE '%John%' then 10 else 0 end) + -- column contains word: priority * 1
        (case when LOWER(`users`.`first_name`) LIKE '%Doe%' then 10 else 0 end) +
        (case when LOWER(`users`.`first_name`) LIKE 'John Doe' then 500 else 0 end) + -- column matches full text: priority * 50
        (case when LOWER(`users`.`first_name`) LIKE '%John Doe%' then 300 else 0 end) + -- column contains full text: priority * 30
        (case when LOWER(`users`.`last_name`) LIKE 'John' then 75 else 0 end) +
        (case when LOWER(`users`.`last_name`) LIKE 'Doe' then 75 else 0 end) +
        (case when LOWER(`users`.`last_name`) LIKE 'John%' then 25 else 0 end) +
        (case when LOWER(`users`.`last_name`) LIKE 'Doe%' then 25 else 0 end) +
        (case when LOWER(`users`.`last_name`) LIKE '%John%' then 5 else 0 end) +
        (case when LOWER(`users`.`last_name`) LIKE '%Doe%' then 5 else 0 end) +
        (case when LOWER(`users`.`last_name`) LIKE 'John Doe' then 250 else 0 end) +
        (case when LOWER(`users`.`last_name`) LIKE '%John Doe%' then 150 else 0 end)
    ) as relevance
    from `users`
    group by `users`.`id`
    having relevance >= 3.75 -- sum of priorities (10 + 5) / 4
    order by `relevance` desc
) as `users`
```

Credits
-------

[](#credits)

- [nicolaslopezj/searchable](https://github.com/nicolaslopezj/searchable)

Contributing
------------

[](#contributing)

Anyone is welcome to contribute. Fork, make your changes, and then submit a pull request.

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community21

Small or concentrated contributor base

Maturity82

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 57.9% 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 ~52 days

Recently: every ~203 days

Total

56

Last Release

1470d ago

Major Versions

0.1.2 → 1.0.02014-06-17

1.9.6 → 2.0.02018-04-16

2.0.3 → v3.02020-02-11

v3.1 → v9.02022-05-05

PHP version history (3 changes)0.1.2PHP &gt;=5.4.0

v3.0PHP ^7.2

v3.1PHP ^7.2|^8.0

### Community

Maintainers

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

---

Top Contributors

[![nicolaslopezj](https://avatars.githubusercontent.com/u/2042567?v=4)](https://github.com/nicolaslopezj "nicolaslopezj (92 commits)")[![curtisweeks](https://avatars.githubusercontent.com/u/1786906?v=4)](https://github.com/curtisweeks "curtisweeks (19 commits)")[![jarnovanleeuwen](https://avatars.githubusercontent.com/u/1358997?v=4)](https://github.com/jarnovanleeuwen "jarnovanleeuwen (5 commits)")[![joostbaptist](https://avatars.githubusercontent.com/u/8181757?v=4)](https://github.com/joostbaptist "joostbaptist (3 commits)")[![tomzx](https://avatars.githubusercontent.com/u/188960?v=4)](https://github.com/tomzx "tomzx (3 commits)")[![JulienTant](https://avatars.githubusercontent.com/u/785518?v=4)](https://github.com/JulienTant "JulienTant (3 commits)")[![kordero](https://avatars.githubusercontent.com/u/1572159?v=4)](https://github.com/kordero "kordero (3 commits)")[![svrnm](https://avatars.githubusercontent.com/u/1519757?v=4)](https://github.com/svrnm "svrnm (2 commits)")[![kerwitz](https://avatars.githubusercontent.com/u/105719?v=4)](https://github.com/kerwitz "kerwitz (2 commits)")[![manavo](https://avatars.githubusercontent.com/u/259487?v=4)](https://github.com/manavo "manavo (2 commits)")[![MangTomas23](https://avatars.githubusercontent.com/u/14972551?v=4)](https://github.com/MangTomas23 "MangTomas23 (2 commits)")[![iget-master](https://avatars.githubusercontent.com/u/9434959?v=4)](https://github.com/iget-master "iget-master (2 commits)")[![n1crack](https://avatars.githubusercontent.com/u/712404?v=4)](https://github.com/n1crack "n1crack (2 commits)")[![OmarMakled](https://avatars.githubusercontent.com/u/3720473?v=4)](https://github.com/OmarMakled "OmarMakled (2 commits)")[![osugregor](https://avatars.githubusercontent.com/u/781984?v=4)](https://github.com/osugregor "osugregor (2 commits)")[![zawilliams](https://avatars.githubusercontent.com/u/1509011?v=4)](https://github.com/zawilliams "zawilliams (1 commits)")[![changeweb](https://avatars.githubusercontent.com/u/9896315?v=4)](https://github.com/changeweb "changeweb (1 commits)")[![doncadavona](https://avatars.githubusercontent.com/u/8132015?v=4)](https://github.com/doncadavona "doncadavona (1 commits)")[![Edilton](https://avatars.githubusercontent.com/u/7880757?v=4)](https://github.com/Edilton "Edilton (1 commits)")[![Evertt](https://avatars.githubusercontent.com/u/1267282?v=4)](https://github.com/Evertt "Evertt (1 commits)")

---

Tags

searchlaraveldatabasemodeleloquentsearchable

### Embed Badge

![Health badge](/badges/jabbtech-searchable/health.svg)

```
[![Health](https://phpackages.com/badges/jabbtech-searchable/health.svg)](https://phpackages.com/packages/jabbtech-searchable)
```

###  Alternatives

[nicolaslopezj/searchable

Eloquent model search trait.

2.0k2.7M35](/packages/nicolaslopezj-searchable)[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)
