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

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

leandrogrando/simple-searchable
===============================

A simple trait to implement eloquent model search method

v1.0(4y ago)3157MITPHP

Since Oct 20Pushed 4y ago1 watchersCompare

[ Source](https://github.com/leandrogrando/simple-searchable)[ Packagist](https://packagist.org/packages/leandrogrando/simple-searchable)[ RSS](/packages/leandrogrando-simple-searchable/feed)WikiDiscussions main Synced 2d ago

READMEChangelog (1)DependenciesVersions (2)Used By (0)

A simple trait to implement eloquent model search method in Laravel
===================================================================

[](#a-simple-trait-to-implement-eloquent-model-search-method-in-laravel)

Simple Searchable is a trait for Laravel that adds a simple search function to Eloquent Models, inspired by the package [nicolaslopezj/searchable](https://github.com/nicolaslopezj/searchable). Simple Searchable allows you to search a table including its relationship fields in belongsTo's relationships. This is not optimized for big searches, but sometimes you just need to make it simple (Although it is not slow).

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

[](#installation)

Run the following command in your application:

```
composer require leandrogrando/simple-searchable

```

Usage
=====

[](#usage)

Add the trait to your model and your search fields (Searchable fields are optional. [You can define them on the fly](#specifying-searchable-fields-on-the-fly).

```
use LeandroGrando\SimpleSearchable\SimpleSearchable;

class User extends Model
{
    use SimpleSearchable;

    /**
     * Searchable fields.
     *
     * @var array
     */
    protected $searchable = [
        'name',
        'email',
        'city.name' // Field in a relationship
        'city.status.name' // Field in a multi relationship
    ];

    public function city()
    {
        return $this->belongsTo('City::class');
    }

}
```

Now you can search your model. The search method accepts two parameters, the first $query is a mandatory string with the search term while the second is an optional $searchable array that if used, will replace the searchable fields of the model with those informed in the array.

```
// Simple search
// $query = 'my search term'
$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('city.state')
            ->get();
```

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

[](#search-paginated)

As easy as laravel default queries

```
// Search with relations and paginate
$users = User::search($query)
            ->with('city.state')
            ->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);
```

Specifying searchable fields on the fly
---------------------------------------

[](#specifying-searchable-fields-on-the-fly)

If necessary, you can pass an array with the searchable fields in the search method itself as the second parameter:

```
$users = User::search($query, [
                'city.name',
                'city.state.name'
                'city.state.uf'
            ])-get();
```

In the example above, the searchable attribute of the User model is ignored and the fields passed in the array of the second parameter are used.

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

[](#contributing)

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

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 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

Unknown

Total

1

Last Release

1665d ago

### Community

Maintainers

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

---

Top Contributors

[![leandrogrando](https://avatars.githubusercontent.com/u/14838027?v=4)](https://github.com/leandrogrando "leandrogrando (5 commits)")

---

Tags

eloquentlaravelmodel-searchphpquery-builder-searchsearch

### Embed Badge

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

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

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

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