PHPackages                             archy-bold/eloquent-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. archy-bold/eloquent-searchable

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

archy-bold/eloquent-searchable
==============================

A service layer to provide support for search on your Eloquent models. WIP only with elasticsearch support currently.

0.0.2(10y ago)72.0kMITPHPPHP &gt;=5.4.0

Since Jun 3Pushed 10y ago2 watchersCompare

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

READMEChangelogDependencies (4)Versions (3)Used By (0)

Eloquent Searchable
===================

[](#eloquent-searchable)

Add search functionality to Eloquent models in Laravel 5.

This package is a WIP and currently only supports very basic text search in the Java-based [elasticsearch](https://www.elastic.co/products/elasticsearch) currently. I will probably add fallback search functionality in the future, but feel free to fork and add additional third-party search engines.

> **NOTE**: This package is missing many features that means it's only useful for the most basic of searches. See the 'Issues' section for a list of known issues and functionality gaps. Improvements to come!

- [Requirements](#requirements)
- [Installation](#installation)
- [Setup Models](#setup)
- [Usage](#usage)
- [Dealing With Errors](#errors)
- [Issues](#issues)

Requirements
------------

[](#requirements)

### PHP &gt;= 5.4.0

[](#php--540)

This pakages uses traits which require PHP 5.4. Laravel 5 also requires PHP 5.4.

### elasticsearch

[](#elasticsearch)

The package currently only supports search through elasticsearch, so you'll need server access and to get that running before you can use this package. Download the elasticsearch server [here](https://www.elastic.co/downloads/elasticsearch).

Java is also required to use elasticsearch.

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

[](#installation)

You can install the package through composer with the following command:

```
$ composer require 'archy-bold/eloquent-searchable:0.*'
```

Next, run `composer update` from your command line to get the dependencies.

Then, update `config/app.php` and add an entry for the service provider.

```
	'providers' => [

		// ...

		'ArchyBold\EloquentSearchable\SearchServiceProvider',

	];
```

To get the default config.php file you must run the following command:

```
$ php artisan vendor:publish
```

Setup
-----

[](#setup)

To make one of your models searchable, you should set it up as follows:

```
use ArchyBold\EloquentSearchable\SearchableModel;
use ArchyBold\EloquentSearchable\SearchableTrait;

class User extends Model implements SearchableModel {

	use SearchableTrait;

	protected $searchable = [
		'columns' => [
			'name', // Reference columns that should be searchable here
			'country.name', // You can reference columns through relationships too.
		],
	];
```

Your model should implement `SearchableModel` and use `SearchableTrait`. You should also include a `$searchable` variable for configuration. Currently this only takes a `columns` argument. This should be an array of columns that should be indexed. You can also reference columns through relationships eg `country.name`.

> **NOTE**: This only currently supports 1:1 or m:1 relationships. 1:m or m:m relationships are not currently supported.

Next add your model to the `search.php` config file.

```
	'models' => [
		'users' => 'App\User',
	],
```

Where the key is a unique name to identify the models in results.

This will allow you to search on models as follows:

```
$user = new User;
$results = $user->search("query string");
```

Where the returned object is an `EloquentCollection` of the results.

> **NOTE**: By default, none of your models will be indexed. They will be automatically indexed when created or updated. If you delete a model, it will be removed from the index too.

Usage
-----

[](#usage)

As well as searching on the model itself (See above), you can search on all searchable models too.

First get an instance of the search provider by getting it from the app layer:

```
$search = app('ArchyBold\EloquentSearchable\SearchProvider');
```

Or use [dependency injection](http://laravel.com/docs/5.0/controllers#dependency-injection-and-controllers) on your controllers.

```
use ArchyBold\EloquentSearchable\SearchProvider;

class UserController extends Controller {

    /**
     * The search provider instance.
     */
    protected $search;

    /**
     * Create a new controller instance.
     *
     * @param  SearchProvider  $search
     * @return void
     */
    public function __construct(SearchProvider $search)
    {
        $this->search = $search;
    }

    // Or with methods injection ...

    public function search(SearchProvider $search)
    {
    	// Use search instance
    }

}
```

You then have access to an instance of a `SearchProvider` (in this case, always an `ElasticSearchProvider`). You can then call use the `searchAll()` function.

```
	$results = $search->searchAll("query string");
	dd($results['users']);
```

This returns an array of `EloquentCollection`s, with each collection containing the results for the search on a different model. The array is indexed by the key you chose for the model in the search config.

Dealing With Errors
-------------------

[](#dealing-with-errors)

If you get the following exception:

```
CouldNotConnectToHost: couldn't connect to host

```

Elasticsearch isn't running. You'll need to start up the server with `bin/elasticsearch` where the `bin/` directory is wherever you installed your elasticsearch instance.

Issues
------

[](#issues)

- Initial indexing - the only models to be indexed are those added after the package has been installed.
- Basic text search only - the package currently only supports very basic searching on text.
- Limited results - only a limited number of results (10) are currently returned and pagination isn't supported.
- Limited search engines - only elasticsearch is currently supported. You need server access and Java installed to use elasticsearch.

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity49

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

Every ~0 days

Total

2

Last Release

4002d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/c69afe3248f40e8ea287b47282c3c98be1abf3038c154a75227feb2ad99da901?d=identicon)[archy-bold](/maintainers/archy-bold)

---

Top Contributors

[![archy-bold](https://avatars.githubusercontent.com/u/5928603?v=4)](https://github.com/archy-bold "archy-bold (7 commits)")

---

Tags

searchlaravelelasticsearcheloquent

### Embed Badge

![Health badge](/badges/archy-bold-eloquent-searchable/health.svg)

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

###  Alternatives

[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[cviebrock/eloquent-sluggable

Easy creation of slugs for your Eloquent models in Laravel

4.0k13.6M253](/packages/cviebrock-eloquent-sluggable)[pdphilip/elasticsearch

An Elasticsearch implementation of Laravel's Eloquent ORM

145360.2k4](/packages/pdphilip-elasticsearch)[cviebrock/eloquent-taggable

Easy ability to tag your Eloquent models in Laravel.

567694.8k3](/packages/cviebrock-eloquent-taggable)[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.

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

PHPackages © 2026

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