PHPackages                             lucasbedout/detective - 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. [HTTP &amp; Networking](/categories/http)
4. /
5. lucasbedout/detective

ActiveLibrary[HTTP &amp; Networking](/categories/http)

lucasbedout/detective
=====================

Provide beautiful and easy to use filtering functionalities for Eloquent library

1.1.3(9y ago)136462PHPPHP &gt;=5.4.0

Since Nov 14Pushed 9y agoCompare

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

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

Detective
=========

[](#detective)

Eloquent extension for advanced filtering. Designed to be used with URL parameters in API context.

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

[](#installation)

```
composer require lucasbedout/detective

```

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

[](#configuration)

Just replace Eloquent by the detective model in your model files.

```
use \Detective\Model as Detective;

class YourModel extends Detective
{
	// Classic eloquent class
}
```

If you need relationship filtering, just list your relationship methods like this.

```
use \Detective\Model as Detective;

class YourModel extends Detective
{
	public static $relationships = ['relation1', 'relation2'];

	public function relation1()
	{
		return $this->belongsTo('Relation1');
	}

	public function relation2()
	{
		return $this->belongsToMany('Relation2');
	}
}
```

Usage example
-------------

[](#usage-example)

You can use the test laravel server provided with the package to test it, you have products, categories and stores table with configured relationships.

*You can see the list of all valid syntaxes at the bottom of this file.*

Add the corresponding route

```
	Route::get('/products', function() {
		$products = Product::filter(Input::all())->get();

		return response()->json($products);
	});
```

Then just query it

```
/GET /products?name=iPhone*

```

Will return a list of products with their name starting with `iPhone` (caution, the search is case sensitive).

You can also query a relationship

```
/GET /products?category-name=Laptop

```

And even a pivot value

```
/GET /products/?category-name=Laptop&stores-pivot-quantity=>5

```

*This query will return all products in the Laptop category with at least one store where the quantity of products is &gt; 5*

You can also use the filter method with a custom array

```
$array = ['name' => 'Home*', 'created_at' => '
