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

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

mrugeshtatvasoft/searchable
===========================

Eloquent model search trait.

v2(1y ago)02MITPHPPHP &gt;=8.1

Since Jan 21Pushed 1y ago1 watchersCompare

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

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

Searchable, a search trait for Laravel
======================================

[](#searchable-a-search-trait-for-laravel)

Searchable is a trait for Laravel 4.2+ and Laravel 5.0 that adds a simple search function to Eloquent Models.

Searchable allows you to perform searches in a table giving priorities to each field for the table and it's relations.

This is not optimized for big searches, but sometimes you just need to make it simple (Although it is not slow).

Installation
============

[](#installation)

Simply add the package to your `composer.json` file and run `composer update`.

```
"mrugeshtatvasoft/searchable": "1.*"

```

Usage
=====

[](#usage)

Add the trait to your model and your search rules.

```
use mrugeshtatvasoft\Searchable\SearchableTrait;

class User extends \Eloquent
{
    use SearchableTrait;

    /**
     * Searchable rules.
     *
     * @var array
     */
    protected $searchable = [
        /**
         * Columns and their priority in search results.
         * Columns with higher values are more important.
         * Columns with equal values have equal importance.
         *
         * @var array
         */
        '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('Post');
    }

}
```

Now you can search your model.

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

// Search and get relations
// It will not get the relations if you don't do this
$users = User::search($query)
            ->with('posts')
            ->get();
```

Search Paginated
----------------

[](#search-paginated)

As easy as laravel default queries

```
// Search with relations and paginate
$users = User::search($query)
            ->with('posts')
            ->paginate(20);
```

Mix queries
-----------

[](#mix-queries)

Search method is compatible with any eloquent method. You can do things like this:

```
// Search only active users
$users = User::where('status', 'active')
            ->search($query)
            ->paginate(20);
```

Custom Threshold
----------------

[](#custom-threshold)

The default threshold for accepted relevance is the sum of all attribute relevance divided by 4. To change this value you can pass in a second parameter to search() like so:

```
// Search with lower relevance threshold
$users = User::where('status', 'active')
            ->search($query, 0)
            ->paginate(20);
```

The above, will return all users in order of relevance.

Entire Text search
------------------

[](#entire-text-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. If you want to prioritize matches that include the multi-word search (thus, without splitting into words) you can enable full text search by setting the third value to true. Example:

```
// Prioritize matches containing "John Doe" above matches containing only "John" or "Doe".
$users = User::search("John Doe", null, true)->get();
```

If you explicitly want to search for full text matches only, you can disable multi-word splitting by setting the fourth parameter to true.

```
// Do not include matches that only matched "John" OR "Doe".
$users = User::search("John Doe", null, true, true)->get();
```

How does it work?
=================

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

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

#### Eloquent Model:

[](#eloquent-model)

```
use mrugeshtatvasoft\Searchable\SearchableTrait;

class User extends \Eloquent
{
    use SearchableTrait;

    /**
     * Searchable rules.
     *
     * @var array
     */
    protected $searchable = [
        'columns' => [
            'first_name' => 10,
            'last_name' => 10,
            'bio' => 2,
            'email' => 5,
        ],
    ];

}
```

#### Search:

[](#search)

```
$search = User::search('Sed neque labore', null, true)->get();
```

#### Result:

[](#result)

```
select `users`.*,

-- If third parameter is set as true, it will check if the column starts with the search
-- if then it adds relevance * 30
-- this ensures that relevant results will be at top
(case when first_name LIKE 'Sed neque labore%' then 300 else 0 end) +

-- For each column you specify makes 3 "ifs" containing
-- each word of the search input and adds relevace to
-- the row

-- The first checks if the column is equal to the word,
-- if then it adds relevance * 15
(case when first_name LIKE 'Sed' || first_name LIKE 'neque' || first_name LIKE 'labore' then 150 else 0 end) +

-- The second checks if the column starts with the word,
-- if then it adds relevance * 5
(case when first_name LIKE 'Sed%' || first_name LIKE 'neque%' || first_name LIKE 'labore%' then 50 else 0 end) +

-- The third checks if the column contains the word,
-- if then it adds relevance * 1
(case when first_name LIKE '%Sed%' || first_name LIKE '%neque%' || first_name LIKE '%labore%' then 10 else 0 end) +

-- Repeats with each column
(case when last_name LIKE 'Sed' || last_name LIKE 'neque' || last_name LIKE 'labore' then 150 else 0 end) +
(case when last_name LIKE 'Sed%' || last_name LIKE 'neque%' || last_name LIKE 'labore%' then 50 else 0 end) +
(case when last_name LIKE '%Sed%' || last_name LIKE '%neque%' || last_name LIKE '%labore%' then 10 else 0 end) +

(case when bio LIKE 'Sed' || bio LIKE 'neque' || bio LIKE 'labore' then 30 else 0 end) +
(case when bio LIKE 'Sed%' || bio LIKE 'neque%' || bio LIKE 'labore%' then 10 else 0 end) +
(case when bio LIKE '%Sed%' || bio LIKE '%neque%' || bio LIKE '%labore%' then 2 else 0 end) +

(case when email LIKE 'Sed' || email LIKE 'neque' || email LIKE 'labore' then 75 else 0 end) +
(case when email LIKE 'Sed%' || email LIKE 'neque%' || email LIKE 'labore%' then 25 else 0 end) +
(case when email LIKE '%Sed%' || email LIKE '%neque%' || email LIKE '%labore%' then 5 else 0 end)

as relevance
from `users`
group by `id`

-- Selects only the rows that have more than
-- the sum of all attributes relevances and divided by 4
-- Ej: (20 + 5 + 2) / 4 = 6.75
having relevance > 6.75

-- Orders the results by relevance
order by `relevance` desc
```

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

[](#contributing)

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

[![Support via Gittip](https://camo.githubusercontent.com/e4c7dd611a46b2f9e453f0693eb0f03dc2aab723b1d42c4bea4a8d86ddadaa80/68747470733a2f2f7261776769746875622e636f6d2f74776f6c66736f6e2f6769747469702d62616467652f302e322e302f646973742f6769747469702e706e67)](https://gratipay.com/mrugeshtatvasoft/)

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance44

Moderate activity, may be stable

Popularity2

Limited adoption so far

Community20

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 66.7% 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 ~0 days

Total

2

Last Release

472d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/393eb85871b607f2faf70252a35b0eb60117bcee6e76446f98bac32b0d44755e?d=identicon)[mktatva](/maintainers/mktatva)

---

Top Contributors

[![nicolaslopezj](https://avatars.githubusercontent.com/u/2042567?v=4)](https://github.com/nicolaslopezj "nicolaslopezj (106 commits)")[![jarnovanleeuwen](https://avatars.githubusercontent.com/u/1358997?v=4)](https://github.com/jarnovanleeuwen "jarnovanleeuwen (5 commits)")[![tomzx](https://avatars.githubusercontent.com/u/188960?v=4)](https://github.com/tomzx "tomzx (3 commits)")[![joostbaptist](https://avatars.githubusercontent.com/u/8181757?v=4)](https://github.com/joostbaptist "joostbaptist (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)")[![rjorel](https://avatars.githubusercontent.com/u/12407789?v=4)](https://github.com/rjorel "rjorel (3 commits)")[![mrugeshtatvasoft](https://avatars.githubusercontent.com/u/50828994?v=4)](https://github.com/mrugeshtatvasoft "mrugeshtatvasoft (2 commits)")[![n1crack](https://avatars.githubusercontent.com/u/712404?v=4)](https://github.com/n1crack "n1crack (2 commits)")[![svrnm](https://avatars.githubusercontent.com/u/1519757?v=4)](https://github.com/svrnm "svrnm (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)")[![kerwitz](https://avatars.githubusercontent.com/u/105719?v=4)](https://github.com/kerwitz "kerwitz (2 commits)")[![caiokawasaki](https://avatars.githubusercontent.com/u/9747004?v=4)](https://github.com/caiokawasaki "caiokawasaki (2 commits)")[![iget-master](https://avatars.githubusercontent.com/u/9434959?v=4)](https://github.com/iget-master "iget-master (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)")[![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)")[![Evertt](https://avatars.githubusercontent.com/u/1267282?v=4)](https://github.com/Evertt "Evertt (1 commits)")

---

Tags

searchlaraveldatabasemodeleloquentsearchable

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/mrugeshtatvasoft-searchable/health.svg)](https://phpackages.com/packages/mrugeshtatvasoft-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.1k4](/packages/jedrzej-searchable)

PHPackages © 2026

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