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)2157MITPHP

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 3w 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 35% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

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

1716d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/14838027?v=4)[Leandro Grando](/maintainers/leandrogrando)[@leandrogrando](https://github.com/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

[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k117.2M118](/packages/jdorn-sql-formatter)[propel/propel1

Propel is an open-source Object-Relational Mapping (ORM) for PHP5.

8351.6M87](/packages/propel-propel1)[jfelder/oracledb

Oracle DB driver for Laravel

11518.4k](/packages/jfelder-oracledb)

PHPackages © 2026

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